字符串的获取相关方法

 1 /*
 2 String当中与获取相关的常用方法有:
 3 
 4 public int length():获取字符串当中含有的字符个数,拿到字符串长度。
 5 public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。
 6 public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始。)
 7 public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。
 8  */
 9 public class Demo02StringGet {
10 
11     public static void main(String[] args) {
12         // 获取字符串的长度
13         int length = "asdasfeutrvauevbueyvb".length();
14         System.out.println("字符串的长度是:" + length);
15 
16         // 拼接字符串
17         String str1 = "Hello";
18         String str2 = "World";
19         String str3 = str1.concat(str2);
20         System.out.println(str1); // Hello,原封不动
21         System.out.println(str2); // World,原封不动
22         System.out.println(str3); // HelloWorld,新的字符串
23         System.out.println("==============");
24 
25         // 获取指定索引位置的单个字符
26         char ch = "Hello".charAt(1);
27         System.out.println("在1号索引位置的字符是:" + ch);
28         System.out.println("==============");
29 
30         // 查找参数字符串在本来字符串当中出现的第一次索引位置
31         // 如果根本没有,返回-1值
32         String original = "HelloWorldHelloWorld";
33         int index = original.indexOf("llo");
34         System.out.println("第一次索引值是:" + index); // 2
35 
36         System.out.println("HelloWorld".indexOf("abc")); // -1
37     }
38 
39 }

 

posted @ 2020-10-18 11:16  Oooooooa  阅读(228)  评论(0)    收藏  举报