String类的两种实例化方法
public class StringDemo01{ public static void main(String args[]){ String name = "liuxun" ; // 实例化String对象 System.out.println("姓名:" + name) ; } };
B:通过keywordnew
public class StringDemo02{ public static void main(String args[]){ String name = new String("liuxun") ; // 实例化String对象 System.out.println("姓名:" + name) ; } };
String类的内容比較
假设直接比較字符串呢? 例如以下所看到的:
public class StringDemo04{ public static void main(String args[]){ String str1 = "hello" ; // 直接赋值 String str2 = new String("hello") ; // 通过new赋值 String str3 = str2 ; // 传递引用 System.out.println("str1 == str2 --> " + (str1==str2)) ; // false System.out.println("str1 == str3 --> " + (str1==str3)) ; // false System.out.println("str2 == str3 --> " + (str2==str3)) ; // true } };
![面向对象基础——String类_字符串](https://s2.51cto.com/images/blog/202108/01/0d860fc6239f5de67734b3990b8db55f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
![面向对象基础——String类_string类_02](https://s2.51cto.com/images/blog/202108/01/0ea53a56abe5a10c16a56c0567f460fc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
假设要想推断其内容是否相等。则必须使用String类中提供的equals()方法完毕,该方法的使用例如以下:
public class StringDemo05{ public static void main(String args[]){ String str1 = "hello" ; // 直接赋值 String str2 = new String("hello") ; // 通过new赋值 String str3 = str2 ; // 传递引用 System.out.println("str1 equals str2 --> " + (str1.equals(str2))) ; // true System.out.println("str1 equals str3 --> " + (str1.equals(str3))) ; // true System.out.println("str2 equals str3 --> " + (str2.equals(str3))) ; // true } };
![面向对象基础——String类_string类_03](https://s2.51cto.com/images/blog/202108/01/8a5941cfe47ee6f5eba83899b9abf3d9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
String类两种实例化方式的差别
public class StringDemo07{ public static void main(String args[]){ String str1 = "hello" ; // 直接赋值 String str2 = "hello" ; // 直接赋值 String str3 = "hello3" ; // 直接赋值 System.out.println("str1 == str2 --> " + (str1==str2)+" "+str1) ; // true System.out.println("str1 == str3 --> " + (str1==str3)+" "+str2) ; // true System.out.println("str2 == str3 --> " + (str2==str3)+" "+str3) ; // true } };
![面向对象基础——String类_字符串_04](https://s2.51cto.com/images/blog/202108/01/0f9f9fd5bfeac9540f4d3c0b8b6dee74.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
![面向对象基础——String类_ide_05](https://s2.51cto.com/images/blog/202108/01/e612a37d92f044272e88bdd0624b3912.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
字符串的内容不可改变
public class StringDemo09{ public static void main(String args[]){ String str = "hello" ; // 声明字符串 str = str + " world!!!" ; // 改动字符串 System.out.println("str = " + str) ; } };
![面向对象基础——String类_赋值_06](https://s2.51cto.com/images/blog/202108/01/2948a982b330127602c335e2e97bb3fe.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
public class StringDemo10{ public static void main(String args[]){ String str1 = "liuxun" ; // 声明字符串对象 for(int i=0;i<100;i++){ // 循环改动内容 str1 += i ; // 字符串的引用不断改变 } System.out.println(str1) ; } };这种操作,要断开连接100次才干够完毕。这种操作性能非常低,应该避免使用。而假设非要使用这种操作,JAVA专门提供了StringBuffer类 专门完毕这种功能。
![面向对象基础——String类_赋值_07](https://s2.51cto.com/images/blog/202108/01/eec3c2ed733c5c9a2aefa609f5d29b1d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
![面向对象基础——String类_string类_08](https://s2.51cto.com/images/blog/202108/01/c623fc01a9c85addc5fabe1275209bd6.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
1字符数组与字符串
public class StringAPIDemo01{ public static void main(String args[]){ String str1 = "hello" ; // 定义字符串 char c[] = str1.toCharArray() ; // 将一个字符串变为字符数组 for(int i=0;i<c.length;i++){ // 循环输出 System.out.print(c[i] + "、") ; } System.out.println("") ; // 换行 String str2 = new String(c) ; // 将所有的字符数组变为String String str3 = new String(c,0,3) ; // 将部分字符数组变为String System.out.println(str2) ; // 输出字符串 System.out.println(str3) ; // 输出字符串 } };
![面向对象基础——String类_ide_09](https://s2.51cto.com/images/blog/202108/01/4cdc0d80cbac8987736a16a6ae986c1b.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
2从字符串中取出指定位置的字符
public class StringAPIDemo02{ public static void main(String args[]){ String str1 = "hello" ; // 定义String对象 System.out.println(str1.charAt(3)) ; // 取出字符串中第四个字符 } };
3字符串与byte数组的转换
public class StringAPIDemo03{ public static void main(String args[]){ String str1 = "hello" ; // 定义字符串 byte b[] = str1.getBytes() ; // 将字符串变为byte数组 System.out.println(new String(b)) ; // 将所有的byte数组变为字符串 System.out.println(new String(b,1,3)) ; // 将部分的byte数组变为字符串 } };
![面向对象基础——String类_string类_10](https://s2.51cto.com/images/blog/202108/01/83cb4c900579a352e09033dfc74def87.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
4取得一个字符串的长度
public class StringAPIDemo04{ public static void main(String args[]){ String str1 = "hello liuxun" ; // 定义字符串变量 System.out.println("\""+str1+"\"的长度为:"+str1.length()) ; } };
5查找指定的字符串是否存在
public class StringAPIDemo05{ public static void main(String args[]){ String str1 = "abcdefgcgh" ; // 声明字符串 System.out.println(str1.indexOf("c")) ; // 查到返回位置 System.out.println(str1.indexOf("c",3)) ; // 查到返回位置,从第4个位置開始查找 System.out.println(str1.indexOf("x")) ; // 没有查到返回-1 } };
![面向对象基础——String类_ide_11](https://s2.51cto.com/images/blog/202108/01/d016a6d3737a99828805321549986e7d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
6、去掉空格
public class StringAPIDemo06{ public static void main(String args[]){ String str1 = " hello " ; // 定义字符串 System.out.println(str1.trim()) ; // 去掉左右空格后输出 } };
![面向对象基础——String类_赋值_12](https://s2.51cto.com/images/blog/202108/01/a45944a731a6b94fe48e15a9b899689a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
7 字符串的截取
public class StringAPIDemo07{ public static void main(String args[]){ String str1 = "hello world" ; // 定义字符串 System.out.println(str1.substring(6)) ; // 从第7个位置開始截取 System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容 } };
![面向对象基础——String类_string类_13](https://s2.51cto.com/images/blog/202108/01/4ad0e755c51d6df787df568bc6ef3a4d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
8 拆分字符串
public class StringAPIDemo08{ public static void main(String args[]){ String str1 = "hello world" ; // 定义字符串 String s[] = str1.split(" ") ; // 按空格进行字符串的拆分 for(int i=0;i<s.length;i++){ // 循环输出 System.out.println(s[i]) ; } } };
![面向对象基础——String类_字符数组_14](https://s2.51cto.com/images/blog/202108/01/250bd220998625ad0c1f7e0e39a0c4aa.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
9 大写和小写转换
public class StringAPIDemo09{ public static void main(String args[]){ System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ; System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ; } };
![面向对象基础——String类_ide_15](https://s2.51cto.com/images/blog/202108/01/83f0bcb40489420c3adba1c736a9dc37.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
10 推断是否以指定的字符串开头或结尾
public class StringAPIDemo10{ public static void main(String args[]){ String str1 = "**HELLO" ; // 定义字符串 String str2 = "HELLO**" ; // 定义字符串 if(str1.startsWith("**")){ // 推断是否以“**”开头 System.out.println("(**HELLO)以**开头") ; } if(str2.endsWith("**")){ // 推断是否以“**”结尾 System.out.println("(HELLO**)以**结尾") ; } } };
![面向对象基础——String类_字符数组_16](https://s2.51cto.com/images/blog/202108/01/90a4f5342cb88f2f72271be6eff1d9cd.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
11 不区分大写和小写的比較
public class StringAPIDemo11{ public static void main(String args[]){ String str1 = "HELLO" ; // 定义字符串 String str2 = "hello" ; // 定义字符串 System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ; System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" " + str1.equalsIgnoreCase(str2)) ; // 不区分大写和小写的比較 } };
![面向对象基础——String类_string类_17](https://s2.51cto.com/images/blog/202108/01/76a5311ce013d366e3c0b9ec619364db.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
12 字符串的替换功能
public class StringAPIDemo12{ public static void main(String args[]){ String str = "hello" ; // 定义字符串 String newStr = str.replaceAll("l","x") ; // 如今将全部的l替换成x System.out.println("替换之后的结果:" + newStr) ; } };
![面向对象基础——String类_ide_18](https://s2.51cto.com/images/blog/202108/01/a936dae886f924b1ca3f1f2d2af2b03c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)