004.字符串

004.字符串

1、采用String作为字符串的类型

比传统char字符数组的方式,更让你更加方便

(例如,调用length方法即可获得长度,无须遍历)

(字符串的连接,在输出时用+即可)

2、字符串结束符的那些事

Java中的string字符串自带/0,为字符串的结尾(c/c++中要自己添加)

3、建议将API文档的方法练习一遍

(1)字符串连接方法


String str_1 = "Oliver";
str_1 = str_1.concat("银色子弹");
System.out.println(str_1);

(2)字符串的比较

如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false。

String str1 = "Oliver";
String str2 = "银色子弹";
StringBuffer str3 = new StringBuffer( "Oliver");

boolean  result;
result = str1.contentEquals( str3 );
System.out.println(result);

result = str2.contentEquals( str3 );
System.out.println(result);

(3)特定返回字符数组中的元素

copyValueOf() 方法有两种形式:

  • public static String copyValueO(char[] data): 返回指定数组中表示该字符序列的字符串。

  • public static String copyValueOf(char[] data, int offset, int count): 返回指定数组中表示该字符序列的 字符串。

char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'O', 'l', 'i', 'v', 'e', 'r'};
String Str2 = "";

Str2 = Str2.copyValueOf( Str1 );
System.out.println("返回结果:" + Str2);

Str2 = Str2.copyValueOf( Str1, 2, 6 );
System.out.println("返回结果:" + Str2);

返回结果:hello Oliver
返回结果:llo Ol

(4)字符串是否以指定内容结尾

如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。

显然,空格也会影响结果

String Str = new String("Oliver 银色子弹");
boolean retVal;

retVal = Str.endsWith( "银色子弹" );
System.out.println("返回值 = " + retVal );

retVal = Str.endsWith( "Oliver银色子弹" );
System.out.println("返回值 = " + retVal );

retVal = Str.endsWith( "Oliver 银色子弹" );
System.out.println("返回值 = " + retVal );

 

(5)字符串与指定的对象比较

如果给定对象与字符串相等,则返回 true;否则返回 false。

String Str = new String("Oliver 银色子弹");
boolean retVal;

retVal = Str.equals( "银色子弹" );
System.out.println("返回值 = " + retVal );

retVal = Str.equals( "Oliver银色子弹" );
System.out.println("返回值 = " + retVal );

retVal = Str.equals( "Oliver 银色子弹" );
System.out.println("返回值 = " + retVal );

(6)intern方法

尽管在输出中调用intern方法并没有什么效果,但是实际上后台这个方法会做一系列的动作和操作。在调用”ab”.intern()方法的时候会返回”ab”,但是这个方法会首先检查字符串池中是否有”ab”这个字符串,如果存在则返回这个字符串的引用,否则就将这个字符串添加到字符串池中,然会返回这个字符串的引用。

可以看下面一个范例:

String str1 = "a";
String str2 = "b";
String str3 = "ab";
String str4 = str1 + str2;
String str5 = new String("ab");

System.out.println(str5.equals(str3));
System.out.println(str5 == str3);
System.out.println(str5.intern() == str3);
System.out.println(str5.intern() == str4);

得到的结果:

true
false
true
false

为什么会得到这样的一个结果呢?我们一步一步的分析。

  • 第一、str5.equals(str3)这个结果为true,不用太多的解释,因为字符串的值的内容相同。

  • 第二、str5 == str3对比的是引用的地址是否相同,由于str5采用new String方式定义的,所以地址引用一定不相等。所以结果为false。

  • 第三、当str5调用intern的时候,会检查字符串池中是否含有该字符串。由于之前定义的str3已经进入字符串池中,所以会得到相同的引用。

  • 第四,当str4 = str1 + str2后,str4的值也为”ab”,但是为什么这个结果会是false呢?先看下面代码:

String a = new String("ab");
String b = new String("ab");
String c = "ab";
String d = "a" + "b";
String e = "b";
String f = "a" + e;

System.out.println(b.intern() == a);
System.out.println(b.intern() == c);
System.out.println(b.intern() == d);
System.out.println(b.intern() == f);
System.out.println(b.intern() == a.intern());

运行结果:

false
true
true
false
true

由运行结果可以看出来,b.intern() == a和b.intern() == c可知,采用new 创建的字符串对象不进入字符串池,并且通过b.intern() == d和b.intern()== f可知,字符串相加的时候,都是静态字符串的结果会添加到字符串池,如果其中含有变量(如f中的e)则不会进入字符串池中。但是字符串一旦进入字符串池中,就会先查找池中有无此对象。如果有此对象,则让对象引用指向此对象。如果无此对象,则先创建此对象,再让对象引用指向此对象。

当研究到这个地方的时候,突然想起来经常遇到的一个比较经典的Java问题,就是对比equal和==的区别,当时记得老师只是说“==”判断的是“地址”,但是并没说清楚什么时候会有地址相等的情况。现在看来,在定义变量的时候赋值,如果赋值的是静态的字符串,就会执行进入字符串池的操作,如果池中含有该字符串,则返回引用。

执行下面的代码:

String a = "abc";
String b = "abc";
String c = "a" + "b" + "c";
String d = "a" + "bc";
String e = "ab" + "c";
       
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a == d);
System.out.println(a == e);
System.out.println(c == d);
System.out.println(c == e);

运行的结果:

true
true
true
true
true
true

转自:莫洛 参考地址 (2017-08-28)

(7)检测两个字符串在一个区域内是否相等。

String Str1 = new String("www.google.com");
String Str2 = new String("google");
String Str3 = new String("GOOGLE");

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str2, 0, 5));

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str3, 0, 5));

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));

(8)字符串的长度

String Str1 = new String("Oliver 银色子弹");
String Str2 = new String("银色子弹" );

System.out.print("字符串 Str1 长度 :");
System.out.println(Str1.length());
System.out.print("字符串 Str2 长度 :");
System.out.println(Str2.length());

(9)返回指定字符/字符串在此字符串中第一次出现处的索引(位置)

  • publicint indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int indexOf(int ch, int fromIndex): 返回从fromIndex位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str, int fromIndex): 返回从 fromIndex位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

字符案例

String string = "aaa456ac";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在

// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6

//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99

// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7

//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  

字符串案例

String Str = new String("谷歌官网:www.google.com");
String SubStr1 = new String("google");
String SubStr2 = new String("com");

System.out.print("查找字符 o 第一次出现的位置 :" );
// 10
System.out.println(Str.indexOf( 'o' ));
System.out.print("从第14个位置查找字符 o 第一次出现的位置 :" );
// 17
System.out.println(Str.indexOf( 'o', 14 ));
System.out.print("子字符串 SubStr1 第一次出现的位置:" );
// 9
System.out.println( Str.indexOf( SubStr1 ));
System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :" );
// -1
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("子字符串 SubStr2 第一次出现的位置 :" );
// 16
System.out.println(Str.indexOf( SubStr2 ));

(10)Java lastIndexOf() 方法

同上,只不过从右往左来搜索

(11)两个字符串在一个区域内是否相等

regionMatches()

如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。是否完全匹配或考虑大小写取决于 ignoreCase 参数

String Str1 = new String("www.google.com");
String Str2 = new String("google");
String Str3 = new String("GOOGLE");

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str2, 0, 5));

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str3, 0, 5));

System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));

(12)字符串是否以指定内容开头

同上,[字符串是否以指定内容结尾]

(13)从字符串中返回一个新的子字符串

substring()

public String substring(int beginIndex)
// 返回从beginIndex开始的原字符串
public String substring(int beginIndex, int endIndex)
// 返回从beginIndex到endIndex之间的内容为一个新字符串
String Str = new String("www.google.com");
// substring n.子链,子串
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );

System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );

(14)判断字符串是否为空

isEmpty()

String myStr1 = "google";
String myStr2 = "";        // 空字符串
String myStr3 = "   ";    // 多个空格,length() 不为 0
System.out.println("myStr1 是否为空:" + myStr1.isEmpty());
System.out.println("myStr2 是否为空:" + myStr2.isEmpty());
System.out.println("myStr3 长度:" + myStr3.length());
System.out.println("myStr3 是否为空:" + myStr3.isEmpty());

myStr1 是否为空:false myStr2 是否为空:true myStr3 长度:4 myStr3 是否为空:false

 

posted @ 2021-02-21 17:30  Oliver--12  阅读(41)  评论(0)    收藏  举报