码迷,mamicode.com
首页 > 编程语言 > 详细

小白的java学习之路 “ 字符串”

时间:2018-06-01 23:20:27      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:必须   用户   输入密码   一个   电话   practice   public   AC   常用   

定一个字符串可以使用string类和stringbuffer类。

string类提供了大量的操作字符串的方法,常用的如下:

获得字符串的长度: length()。

比较字符串:equals()。

链接字符串:concat()。

提取字符串:substring()。

搜索字符串:indexOf()。

拆分字符串:spilt()。

常用的stringbuffer类提供的方法如下:

转换成string类型:tostring()

链接字符串:append()。

插入字符串:intsert()。

一些案例:

public class Demo_1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String pwd;
        @SuppressWarnings("resource")
        Scanner input =new Scanner(System.in);
        do{
        System.out.println("请输入用户名:");
        String name = input.next();
        System.out.println("请输入密码:");
        pwd= input.next();
        if(pwd.length()<6){
            System.out.println("密码长度不够");
        }else{
            System.out.println("输入正确");
            System.out.println("欢迎你!"+name);
            break;
        }
        }while(pwd.length()!=6);
    }
boolean flag ;
    public  void panduan(String sfz , String sjh,String zj) {
        flag=false;
        int index=sjh.lastIndexOf("-");
        
    if(!(sfz.length()==16||sfz.length()==18)){
        System.out.println("身份证号必须是16位或18位!");
    }
    else if(sjh.length()<11){
        System.out.println("手机号必须是11位!");
    }else if (index!=-1&&index!=0&&index!=4&&zj.length()!=13) {
        System.out.println("座机号码区必须为4位,电话号码必须是7位!");
    }else{
    System.out.println("注册成功");
    flag=true;
          }

    }





调用:
public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner input=new Scanner(System.in);
        Practice2 pd =new Practice2();
        do {
            System.out.println("请输入身份证:");
            String sfz = input.next();
            System.out.println("请输入手机号码:");
            String sjh = input.next();
            System.out.println("请输入座机号:");
            String zj = input.next();
            pd.panduan(sfz, sjh, zj);
        } while (pd.flag==false);
    }

 

小白的java学习之路 “ 字符串”

标签:必须   用户   输入密码   一个   电话   practice   public   AC   常用   

原文地址:https://www.cnblogs.com/wishsaber/p/9123673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!