Java之String类(中)

String类获取功能的方法       

1、- public int length():返回此字符串的长度

// 创建字符串对象
String s1 = "hello-world!";

//获取字符串的长度(字符串字符的个数)
int length = s1.length();
System.out.println("s1字符串的长度是:"+length);// 12


2、- public String concat(String str):将指定的字符串连接到该字符串的末尾

// 创建字符串对象
String s1 = "hello-world!";
//获取字符串的长度(字符串字符的个数)
int length = s1.length();
System.out.println("s1字符串的长度是:"+length);// 12
// s1字符串的末尾拼接上hello-donglan
String s2 = s1.concat("hello-donglan");
System.out.println("拼接后的字符串s2:"+s2);// hello-world!hello-donglan


3、- public char charAt(int index):返回指定索引处的char值

// 创建字符串对象
String s1 = "hello-world!";

// 获取s1中索引值为1的字符
char c = s1.charAt(1);
System.out.println("索引值为1的字符c:"+c);


4、- public int indexOf(String str):返回指定字符串第一次出现在该字符串内的索引

// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";
// 查找hello第一次出现在s3中的索引位置
int index1 = s3.indexOf("hello");
System.out.println("hello第一次出现在s3中的索引值位置:"+index1);


5、- public int indexOf(String str,int fromIndex):返回指定索引位置查找,

        该字符串第一次出现在该字符串内的索引

// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";
// 查找hello第一次出现在s3中的索引位置
int index1 = s3.indexOf("hello");
System.out.println("hello第一次出现在s3中的索引值位置:"+index1);
// 查找hello第二次出现在s3中的索引位置
int index2 = s3.indexOf("hello",index1+1);
System.out.println("hello第二次出现在s3中的索引值位置:"+index2);
// 查找hello第三次出现在s3中的索引位置
int index3 = s3.indexOf("hello",index2+1);
System.out.println("hello第三次出现在s3中的索引值位置:"+index3);

6、- public int lastIndexOf(String str):返回指定字符串最后一次出现在该字符串内         的索引


// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";

// 查找hello最后一次出现的索引位置
int lastIndex = s3.lastIndexOf("hello");
System.out.println("hello最后一次出现的索引位置:"+lastIndex);

7、- public int lastIndexOf(String str,int fromIndex):返回从指定索引位置查找,
       该字符串最后一次出现在该字符串内的索引

// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";

// 查找hello最后一次出现的索引位置
int lastIndex = s3.lastIndexOf("hello");
System.out.println("hello最后一次出现的索引位置:"+lastIndex);

// 查找hello倒数第二次出现的索引位置
int lastIndex1 = s3.lastIndexOf("hello",lastIndex-1);
System.out.println("hello最后一次出现的索引位置:"+lastIndex1);ndex3);

8、- public String subString(int beginIndex):返回一个字符串,从beginIndex开始
       截取字符串到字符串尾

// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";

// 获取hello-donglan字符串
String s4 = s3.substring(12);
System.out.println("截取字符串:"+s4);

9、- public String subString(int beginIndex,int endIndex):返回一个字符串,
       从beginIndex到endIndex截取字符串
            【包含beginIndex,不包含endIndex】

// 创建一个比较长的字符串对象
String s3 = "hello-world-hello-donglan-hello-java-hello-shanghai";

// 获取hello-donglan字符串
String s4 = s3.substring(12);
System.out.println("截取字符串:"+s4);

String s5 = s3.substring(12,25);
System.out.println("截取中间字符串:"+s5);

10.案例:

public class Tests {
    public static void main(String[] args) {
        /*
        * 需求:
        *       已知用户名和密码,请用程序实现模拟用户登录,总共三次机会,登录之后给出相应的提示
        *
        *
        * 分析:
        *       1.已知用户名和密码,定义两个字符串
        *       2.键盘录入用户登录的用户名和密码,用Scanner
        *       3.键盘录入的登录用户名和密码 和 已知的用户名和密码 进行比较 ,equals比较
        *       4.使用循环实现三次登录的机会,知道循环次数,所以for循环,如果登录成功,使用break终止循环
        *
        * */
        // 用户名 : yonghuming    密码 : 123456
        String user_Name = "yonghuming";
        String name ;

        String password = "123456";
        String pw ;
        Scanner scanner = new Scanner(System.in);
        for(int i = 1 ; i<4;i++) {
            System.out.println("请输入用户名");
             name=scanner.next();
            System.out.println("请输入密码");
            pw = scanner.next();
            if (user_Name.equals(name)&&password.equals(pw)) {
                System.out.println("输入正确");
                break;
            } else{
                System.out.println("输入错误,请重新输入,你还有"+(3-i)+"机会");
                if(3-i==0){
                    System.out.println("三次输入都错误");
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值