[TOC]

一、字符串的介绍及视频解说

  1. 字符串是常量,表明办法:用双引号””引起来
  2. 字符串的创立之后就不能修正,每逢你觉得字符串发生了改变那就是创立了新的字符串

点击这儿去B站观看视频

二、字符串的两种创立办法

办法一:经过new创立

StringM X y str = new SY [ ? J @ )tring("abcd");

办法二:直接创立

String str1 = ("abcd"C b 5);

三、字符串的长度获取:length()办法

String str1 = ("abcd");
int length = str1.length();
System.out.println("abcd的长度为:"+length);//4

四、使用 == 和equals()办法比较两个字符串

1. 用 == 比较两个字符串(由此分析字符串的两种创立办? / ( 3 R a D法的不同)

关于引证类型来说,== 进行的是地址值的` : 0 [ # 6 b %比较

		String str = new String("abcd");//第一种创立办法:经过nD W v ~ )ew创立
String str1 = new String("abc3 r 1d");//第一种创立办法:经过nE H B n 9 & I ) Zew创立
String stra = "abcd";//第二种创立办法:直接创立
String strb = "abcd";//第二种创立办法:直接创立
System.out.println(str == str1);//new出的两个字符串目标之间比较,false
System.out.println(str == stra);//new出的字符串目标与直接创立的字符串目标之间比较,false
System.out.printlnE m @ 8(stra == strb);//直接创立的两个字符串目标u w W S N m 3之间比较,true

(1)关于用new办法创立出的两个字符串之间比较

System.out.printlnI w P | : o Q m(` O qstr == str1);//new出的两个字符串目标之间比较,false的代码运转成果咱们能够看出:

		String str = new String("abcd");//第一种创立办法:经过new创立
String sT h k } 8 [ Ptr1 = newq , u f 2 I String("abcd` & i j");//第一种创立办法:经过new创立
视频+图文  String类干货向总结
  • 经过new创立出的str和str1字符j Y t串目标虽内容相同
  • 由于是new出的是两个不同的目标所以它们的地址值必然不同

关于引证类型来说 == 比较的是地址值

  • 所以运转成果就为false

(2)关于直接创立出的两个字符串之间比较

System.out.printl@ . Wn(stra == sT $ 2 r f ; : $ 2trb);//直接创立的两个字符y : F 5 Y [ V串目标之间比较,true的代码运转成果咱们能够看出:

		String stra = "abcd";//第二种创立办法:直接创立
String strb = "abc$ S 3 , t P pd";//第二种创立办法:直接创立
System.outu y 4.println(stra == strb);//true
视频+图文  String类干货向总结
  1. 直接创立出的stra和strb字符串x W u q ? w W目标内容相同地址值相同

  2. 原因如下:

    直接创立的字符串目标保存在字符串常量池

    字符串常量池当中不会呈现重复字符串目标Y v B P #对应的地址,这样能够确保字符串不重复

  3. 所以 strastrb地址值相同的,运转成果就为true

(3)直接创立出的字符串与new创立出的字符串之间比较

System.out.pri[ s ( F q - S $ Cntln(str == stra);//nen G L rw出的字符串与直接创立的字符串目标之间比较,false的代码运转成果咱们能够看出:

		String str = new String("abcd");//第一种创立办法:经过new创立
String stra = "abcd";//第二种创立办法:直接创立
视频+图文  String类干货向总结
  • 经过= W M R 2 q wnew创立出的str和直接创立的stra字符串目标虽内容相同

  • 但new出的} C T J W V字符串目* ; . Y _存放在中,一个是直接创立字符串目标存放在字符串常量池

  • 所以它们的b # _址值必然不同

  • 所以运转成果就为false

2. 用e J S , ` Jquals()办法比较两个字符串:比较的是两个字符串的* – ! A是否相同

  • 因为这4个字符串的内容均为”abcd”
  • 所以用equals()| g t办法比较时,成果均为true
		Stri} | D { s / P q `ng str = new String("abcd");
String str1 = new String("abcd");
String stra = "abcd";
String strb = "abcd";
System.out.println(str.equals(s% L d ! ` ftr1));//true
System.out.println(str.equals(stra));//tru5 ! c Y ne
System.out.println(stra.equals(strb));//true

五、字符串的衔接

(1)用“+”衔接:可与不同类型相连

		String str = new String7 a *("abcd");
int a = 12;
String b = a + str;
System.out.println(b);//12abcd

(2)用concat()办法衔接:只能用于字符串与字符串的衔接

视频+图文  String类干货向总结
		String str1 = new String("abcd");
String str2 = str.concat(str1);
System.out.println(: f / r ~ 3 ? vstr1);//abcd5 u 9 h m J
System.out.println(str2);//abcdabH p l k 1 .cd

从上面的 str1 和 str2 输出成果 能够很好的了解前面说的:

字符串的创立之后就不能修正,每逢V a : B . A你觉得字符串发生了改变那就是创立了新的U J S P 6字符串s C v } x

六、判别一个字符串是否包括指定字符串

视频+图文  String类干货向总结

这儿的“char值序列说的比较艰深”。

在初期,咱们能够直接了解为使用contD } ~ F 0 Eains()办法能够:

  • 查看一个字符串中是否包括指定的字符串

  • / + ! S 6:下面的例子是判别 字符串str是否 包括E N M 8 % ? : I “a# . j 7 Z B &b”

   	String str = new String("abcd");
boolean judo S 6 !ge = str.contains("ab");
System.out.println(judge);//true

七、字符串的大小写转化

(1)大写字符串转化为小写

视频+图文  String类干货向总结

String strh V Tc = ("Ay x M b 7BREa");
System.out.O  R N l , - &println(strc);//ABREa
System.out.println(strc.toLowerCaseX 6 ` 9 j n(););//abrea

从上面的 s3 I i = E 3 % xtrc 和 strc.toLowerCase() 输出成果 能够很好的了解前面说的:

字符串的创立之后就不能修正,每逢你觉得字符串; K ^ | S G b发生了改变那就是创立了新的字符串

(2)小写字符串转化为大写R N D j 5 W v

视频+图文  String类干货向总结
		String str = new String("abcd");
System.out.prn t q n Jintln(st& F = , [ / zr);//ab? K $ 4 rcd
System.P [ V V Z ( o =out.prx A j vintln(str.toUpperCase(););//ABCD

从上面的 str 和 s| 3 d V v Ktr.toUpperCase() 输出成果~ # c & 能够很好的了解前面说| – /的:

字符串的创立之后就不能修正,每逢你觉得字符串发生了改变那就是创立了新的字符串

八、得到指定索引对应的字符

视频+图文  String类干货向总结

留意:字符串的索引从0开端

		String str = new String("abcd");
System.out.println(str);//abcd
System.o( q N b aut.println(str.cha% / 5 K 3 @ &rAt(1));//b

从上面的 str 和 str.charAt(1) 输出成果 能够很好的了解前面说的:

字符串的创立之后就不能修正,每逢你觉得字符串发生了改变那就是创立了新的字符串

九、得到指定字符串初次呈现的索引

从下面两种转化办法的输出成果 能够很好的了解前面说的:

字符串的创立之后就不能修正,每逢你觉得字符串发生了改变那就是创立了新的字符串

第一种:

视频+图文  String类干货向总结
		String str4 = "012345618a9";
int index = str4.indexOf("1");//找到字符串"1"初次呈现的索引
System.out.println(index2);//1
Sy& J m ? H Rstem.out.printly : _ V j X wn("========a C @ q=============");
int index1 =x h c  U str$ q U i  p4.indexOf("1", index + 1);y U [ D//从index2 +y L X [ x 1 I ^ [ 1开端找字符串"1"初次呈现的索引
System.out.println(indy $ 7 A * ] jex3);//7
  • 当index2=1时即索引1记录的是子串”1″第一g j b {次呈现的方位
  • 所以在找子串”1″第2次呈现的方位时要I m v q G F从index2+? J 5 D L & A V =1的方位开端找
  • 因为+ / { Y Kindex2的方位现已找过了
  • 要是 还从index2=1的方位找,那么找到的索引仍是为1
    视频+图文  String类干货向总结

第二种: ( ! B z j b

视频+图文  String类干货向总结
		String str5 = "012345612a9";
int ind2 n U 8 a [ 2ex4 = str5.iZ k jndexOf('2');//找到字符'2'初次呈现的索引
System.out.println(index4);//2
System.out.println("============S M Y _ 2=========");~ u r ( I _ 0 $ N
int index5 = str5.indexOf('2', index4 + 1);//从index4 + 1开端找字符'2'初次呈现的索引
System.ou, o g 1 S it.println(index5);T ` B & a 3 !//8
  • 当index4=2时即索引2记录的是字符’N D z s 9 l –2’第一次呈现g @ X w $ ^的方V h : K M ? x (2 V O # 2 & 7 {
  • 所以在找字符’2’第2` C f n J t a呈现的方位时要从index4+1的方位开端找
  • 因为index4的方位现已找过了
  • 要是 还从index4=1的方# D c位找,那么找到的索引仍是& y & z ? Z u为2
视频+图文  String类干货向总结

十、字符串f M R ] r J转化字符数组及字符串与根本数据类型相互转化

超具体解说看这儿~~

(一)字符串转化字符数组

视频+图文  String类干货向总结
		String str = new St z , J u 8 Hring("abcd");
cha6 u Z W w Jr ch[] = str.toCharArray();//将字符串转化为字符数组
for(int i =0;i<ch.length;i++) {//遍历得到的字符数组
System.out.pr- K ( + d s # 1ind E W jtln(ch[i]);
}

(二)字符串与根本数据类型相互转化

1.字符串转化为根本数据类型:以字符串转化T R + , O j 5为int型为例

		//字符串转化成int型
String str ! , f c + [ ` 7e = "123456";
int a1 = Integer.parseInt(stre);//对应的包装类.parsexxx 办法b n d G ~ J 1
System.out.println(a1+2);//123458,证明是int型

2.根本数据类型转化为字符串:以int型转化为字符串为例

		//int型转T b 0 o  Y y I化成字符串
int num =123;
String strs = String.valueOf(num);// String.valueOf()办法
System.out.println(strs+12);//12312,证明是字符串

到此字符串的干货就总结完啦~~

欢迎大家来公号 “小乔的编程k ` 5 ; e d (内容共享站”
来找小乔玩~
一起学习Java+算法~
还有更多资源等你来拿哦~

视频+图文  String类干货向总结