String 类的各种方法汇总

使用String对象存储字符串【字符串存在字符串池里,因为字符串太常用了所以所有一样的字符串都会调用同样的字符串,所以在字符串池中的字符串对象都是一样的】

String s = "Hello World";

String s = new String();

String s = new String("Hello World");

String类位于java.lang包中,具有丰富的方法

计算字符串的长度、比较字符串、连接字符串、提取字符串

一、length()方法:

String类提供了length()方法,确定字符串的长度,返回字符串中的字符数

注意其与数组的length【属性】的区别

pwd.length()<6

二、equals()方法:

String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致

userName.equals("Tom");

equals()方法比较原理:

equals(): 检查组成字符串内容的字符是否完全一致(每一个字符串的每一个字符逐次比较,全部完全一样才返回true)。

==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象。

 

创建了几个对象的问题:

              String str1 = "hello";

              String str2 = new String("Hello");

              System.out.println(str1.equals(str2));  //false

              System.out.println(str1 == str2);     //false

创建了3个对象字符串池里创建了2个对象,堆里创建了1个对象

 

              String str1 = "hello";

              String str2 = new String("hello");

              System.out.println(str1.equals(str2));  // true

              System.out.println(str1 == str2);     //false

创建了2个对象字符串池里创建了1个对象,堆里创建了1个对象

 

       String s1 = "hello";

       String s2 = "hello";

       System.out.println(s1.equals(s2));  //true

       System.out.println(s1 == s2);     //true

只在字符串池里创建了1个对象

 

              String str1 = new String("Hello");

              String str2 = new String("Hello");

              System.out.println(str1.equals(str2));  // true

              System.out.println(str1 == str2);     //false

创建了3个对象堆里创建了2个对象,字符串池里创建了1个对象

三、equalsIgnoreCase()方法

忽略大小写进行比较

              String str1 = new String("Hello");

              String str2 = new String("Hello");

              System.out.println(str1.equals(str2));  // true

四、toLowerCase()方法  

全变成小写

              String str1 = new String("hello");

              String str2 = new String("HELLO");

              String str3 =str2.toLowerCase(str2);

              System.out.println(str1.equals(str3));  // true

五、toUpperCase()方法

全变成大写

              String str1 = new String("HELLO");

              String str2 = new String("hello");

              String str3 =str2.toUpperCase(str2);

              System.out.println(str1.equals(str3));  // true

六、trim()方法

去掉字符串两边的空格   

              String str1 = new String("hello");

              String str2 = new String("    hello   ");

              String str3 =str2.trim();

              System.out.println(str1.equals(str3));  // true

七、字符串连接方法

方法1:使用“+”

方法2:使用String类的concat()方法

              int num = 9;
              System.out.println("num的值是:"+num);
              String s1 = "hello";
              String s2 = "heleelo";
              String s3 = s1.concat(s2);
              System.out.println(s3);

八、indexOf :

搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1

(空格也算一个位置,多少个空格算多少个位置!!!)

              String stest="helloabcworldabcjamabcmaryabcend";
              int index = stest.indexOf("abc", i);
              System.out.println(index);


九、lastlndexOf

搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1

(空格也算一个位置,多少个空格算多少个位置!!!)

              String stest="helloabcworldabcjamabcmaryabcend";
              int index = stest.lastIndexOf("abc", i);
              System.out.println(index);

十、substring

提取从位置索引开始到最后的字符串部分

(从第几个位置开始截取包含起始的这个位置)

              String stest="helloabcworldabcjamabcmaryabcend";
              String temps =stest.substring(4);
              System.out.println(temps);

十一、substring(int beginindex, int endindex)  

提取beginindex和endindex之间的字符串部分

(截取时,包含起始位置,但是不包含结束位置【起始位置----结束位置-1】)

              String stest="helloabcworldabcjamabcmaryabcend";
              String temps =stest.substring(4,8);
              System.out.println(temps);

十二、split()

将这个字符按照给定的方法分割

              String words = "长亭外 古道边 啦啦啦啦啦 啦啦 啦 啦";
              String[] printWords = new String[20];
              System.out.println("原歌词");
              System.out.println(words);
              printWords = words.split(" ");
              System.out.println("拆分后");
              for(String info : printWords){
              System.out.println(info);}

十三、startsWith(String prefix)

测试此字符串是否以指定的前缀开头。

              String stest="helloabcworldabcjamabcmaryabcend";
              System.out.println(stest.startsWith("h"));    //true

十四、valueOf()

除Character类外,其他包装类都有如下方法(字符串->包装类)

             Integer intValue = Integer.valueOf("21");

十五、toString()

以字符串形式返回包装对象表示的基本类型数据【基本类型->字符串】

              String sex=Character.toString(‘男’);

              String id=Integer.toString(25);

或者:

              String sex = ‘男’+ “”;

              String id = 25 + “”;

 

猜你喜欢

转载自blog.csdn.net/baidu_29343517/article/details/81299207