JAVA的字符串


定一个字符串可以使用string类和stringbuffer类。string类提供了大量的操作字符串的方法,常用的如下:获得字符串的长度: length()。比较字符串:equals()。链接字符串:concat()。提取字符串:substring()。搜索字符串:indexOf()。拆分字符串:spilt()。常用的stringbuffer类提供的方法如下:转换成string类型:tostring()。链接字符串:append()。插入字符串:intsert()。
一些案例:
public class Register {
    public static void main(String[] args) {
        Scanner input  =  new Scanner (System.in);
        System.out.println("请输入用户名:");
        String name = input.next();
        System.out.println("请输入密码:");
        String pwd = input.next();
        if (name.equals("TOM")&&pwd.length()>6){
            System.out.println("注册成功!");
        }else{
            System.out.println("密码长度不能小于六位!请重新输入:");
            pwd=input.next();
        }
    }
2.public int verify(String name, String pwd1, String pwd2){
        int app=1;
        if ((pwd1.length()<6&&name.length()<3)){
            System.out.println("姓名长度不能小于三,密码长度不能小于六");
        }else if(!pwd1.equals(pwd2)){
            System.out.println("两次输入的密码不相同");
        }else{
            System.out.println("注册成功!");
            app=2;
        }
        return app;
2.2public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        int c;
        Asdss adss = new Asdss();
        
        
        do {
            System.out.println("请输入用户名:");
            String name=input.next();
            System.out.println("请输入密码:");
            String mima1=input.next();
            System.out.println("请再次输入密码:");
            String mima2=input.next();
            
            c=adss.verify(name, mima1, mima2);
        } while (c==1);
        
    }

猜你喜欢

转载自www.cnblogs.com/ws1149939228/p/9124334.html