String

Posted 旗正飘飘 马正萧萧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了String相关的知识,希望对你有一定的参考价值。

初稿:2017-11-19 23:57:11

String的构造方法

 1 import java.nio.charset.Charset;
 2 import java.io.UnsupportedEncodingException;
 3 
 4 public class StringContructorTest {
 5 
 6     public static void main(String[] args) {
 7         testStringConstructors() ;
 8     }
 9     private static void testStringConstructors() {
10         try {
11             System.out.println("------------------------ testStringConstructors -----------------------");
12 
13             String str01 = new String();
14             String str02 = new String("String02");
15             String str03 = new String(new char[]{\'s\',\'t\',\'r\',\'0\',\'3\'});
16             String str04 = new String(new char[]{\'s\',\'t\',\'r\',\'0\',\'4\'}, 1, 3); 
17             String str05 = new String(new StringBuffer("StringBuffer"));
18             String str06 = new String(new StringBuilder("StringBuilder"));
19             String str07 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65});
20             // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
21             String str08 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 1, 3);
22              // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
23             String str09 = new String(new byte[]{(byte)0xe5, (byte)0xad, (byte)0x97, 
24                                                  (byte)0xe7, (byte)0xac, (byte)0xa6, 
25                                                  (byte)0xe7, (byte)0xbc, (byte)0x96, 
26                                                  (byte)0xe7, (byte)0xa0, (byte)0x81}, 
27                                       0, 12, "utf-8");  // 0表示起始位置,12表示长度。
28             String str10 = new String(new byte[]{(byte)0x5b, (byte)0x57,  
29                                                  (byte)0x7b, (byte)0x26, 
30                                                  (byte)0x7f, (byte)0x16, 
31                                                  (byte)0x78, (byte)0x01}, 
32                                       0, 8, "utf-16");  // 0表示起始位置,8表示长度。
33             String str11 = new String(new byte[]{(byte)0xd7, (byte)0xd6, 
34                                                  (byte)0xb7, (byte)0xfb,
35                                                  (byte)0xb1, (byte)0xe0, 
36                                                  (byte)0xc2, (byte)0xeb}, 
37                                       Charset.forName("gb2312")); 
38             String str12 = new String(new byte[]{(byte)0xd7, (byte)0xd6, 
39                                                  (byte)0xb7, (byte)0xfb,
40                                                  (byte)0xb1, (byte)0xe0, 
41                                                  (byte)0xc2, (byte)0xeb}, 
42                                       0, 8, Charset.forName("gbk")); 
43             String str13 = new String(new int[] {0x5b57, 0x7b26, 0x7f16, 0x7801},0,4);
44             //"字符编码"(\\u5b57是‘字’的unicode编码)。0表示起始位置,4表示长度。
45             System.out.println("str01 = " + str01);
46             System.out.println("str02 = " + str02);
47             System.out.println("str03 = " + str03);
48             System.out.println("str04 = " + str04);
49             System.out.println("str05 = " + str05);
50             System.out.println("str06 = " + str06);
51             System.out.println("str07 = " + str07);
52             System.out.println("str08 = " + str08);
53             System.out.println("str09 = " + str09);
54             System.out.println("str10 = " + str10);
55             System.out.println("str11 = " + str11);
56             System.out.println("str12 = " + str12);
57             System.out.println("str13 = " + str13);
58         } catch (UnsupportedEncodingException e) {
59             e.printStackTrace();
60         }
61     }
62 }
View Code

String的常用方法

int compareTo(String str)   如果equals,返回0,如果出现字符不匹配,返回Unicode码值差,如果满足子串关系,返回方法串.len - 参数串.len

int compareToIgnoreCase(String str) 同上,但不区分大小写

boolean equals(String str)  如果完全相同,返回true,否则返回false

boolean equalsIgnoreCase(String str) 同上,但是不区分大小写

boolean startsWith(String str) 是不是以str开头

boolean endsWith(String str) 是不是以str结尾

boolean contains(String str)是不是包含子串str

String  toUpperCase() 转成大写

String toLowerCase() 转成小写

boolean contentEquals(StringBuffer str) 

boolean contentEquals(StringBuilder str)

String concat(String str) 调用串后接参数串并返回

String substring(int startindex, int endindex) 截取一个子串

String replace(\'单个字符\' , \'单个字符\') 把所有的某个特定字符转换成另外一个字符并返回

String replace("单个字符或字符串",“单个字符或字符串”) 替换 并返回

String[ ] split("字符串") 将字符串以某个字符串作为分界线,拆分成多个子字符串并返回字符串数组

 1. 字符串长度

int = str.length()

2. 字符串是否为空

boolean str.isEmpty()

3. [字节] 获取字符串对应的字节数组

byte[] barr = str.getBytes()

4. [字符] 获取字符串位置4的字符

char str.charAt(4)
5.[字符] 获取字符串对应的字符数组 char[] carr = str.toCharArray();
   
 1 -------------------------------- testIndexAPIs --------------------------------
 2 istr=abcAbcABCabCaBcAbCaBCabc
 3 istr.indexOf((int)\'a\')         = 0
 4 istr.indexOf((int)\'a\', 5)      = 9
 5 istr.lastIndexOf((int)\'a\')     = 21
 6 istr.lastIndexOf((int)\'a\', 10) = 9
 7 istr.indexOf("bc")             = 1
 8 istr.indexOf("bc", 5)          = 22
 9 istr.lastIndexOf("bc")         = 22
10 istr.lastIndexOf("bc", 4)      = 4
11 
12 
13 -------------------------------- testCompareAPIs ------------------------------
14 str=abcAbcABCabCAbCabc
15 str.equals("abcAbcABCabCAbCabc")                   = true
16 str.equalsIgnoreCase("ABCABCABCABCABCABC")         = true
17 str.compareTo("abce")                    = -36
18 str.compareToIgnoreCase("ABC")           = 15
19 str.startsWith("ab")                     = true
20 str.startsWith("Ab")                     = true
21 str.endsWith("bc")                       = true
22 str.contains("ABC")                      = true
23 regionMatches(cAbcABCabCAbCabc) = true
24 regionMatches(CABCABCABCABCABC) = false
25 regionMatches(cabcabcabcabcabc) = false
26 str.contentEquals(new StringBuffer("abcAbcABCabCAbCabc"))    = true
27 str.contentEquals(new StringBuilder("abcAbcABCabCAbCabc"))   = true
28 ipv4addr1.matches()                      = true
29 ipv4addr2.matches()                      = false
30 
31 
32 -------------------------------- testModifyAPIs -------------------------------
33 str= abcAbcABCabCAbCabc , len=20
34 str.concat("123")              =  abcAbcABCabCAbCabc 123
35 str.substring(7)               = ABCabCAbCabc 
36 str.substring(7, 10)           = ABC
37 str.trim()                     = abcAbcABCabCAbCabc, len=18
38 str.replace(\'a\', \'M\')          =  _bcAbcABC_bCAbC_bc 
39 str.replaceFirst("a", "###")   =  ###bcAbcABCabCAbCabc 
40 str.replace("a", "$$$")        =  $$$bcAbcABC$$$bCAbC$$$bc 
41 splits[0]= a
42 splits[1]=cA
43 splits[2]=cABCa
44 splits[3]=CA
45 splits[4]=Ca
46 splits[5]=c
47 
48 -------------------------------- testUnicodeAPIs ------------------------------
49 ustr=字符编码
50 ustr.codePointAt(0)            = 0x5b57
51 ustr.codePointBefore(2)        = 0x7b26
52 ustr.offsetByCodePoints(1, 2)  = 3
53 ustr.codePointCount(0, 3)      = 3
54 
55 -------------------------------- testOtherAPIs --------------------------------
56 str=0123456789
57 str.length() = 10
58 str.isEmpty() = false
59 barr[0]=0x30 barr[1]=0x31 barr[2]=0x32 barr[3]=0x33 barr[4]=0x34 barr[5]=0x35 barr[6]=0x36 barr[7]=0x37 barr[8]=0x38 barr[9]=0x39 
60 str.charAt(4) = 4
61 carr[0]=0 carr[1]=1 carr[2]=2 carr[3]=3 carr[4]=4 carr[5]=5 carr[6]=6 carr[7]=7 carr[8]=8 carr[9]=9 
62 carr2[0]=6 carr2[1]=7 carr2[2]=8 
63 str.copyValueOf(new char[]{\'a\',\'b\',\'c\',\'d\',\'e\'}) = abcde
64 str.copyValueOf(new char[]{\'a\',\'b\',\'c\',\'d\',\'e\'}, 1, 4) = bcde
65 str.format() = abc-3-true
View Code

 

String将各种对象转换成String的API

 1 import java.util.HashMap;
 2 public class StringValueTest {
 3     public static void main(String[] args) {
 4         testValueAPIs() ;
 5     }
 6     private static void testValueAPIs() {
 7         System.out.println("-------------------------------- testValueAPIs --------------------------------");
 8         // 1. String    valueOf(Object obj)
 9         //  实际上,返回的是obj.toString();
10         HashMap map = new HashMap();
11         map.put("1", "one");
12         map.put("2", "two");
13         map.put("3", "three");
14         System.out.println("String.valueOf(map)   "+String.valueOf(map));
15         // 2.String    valueOf(boolean b)
16         System.out.println("String.valueOf(true)   "+String.valueOf(true));
17         // 3.String    valueOf(char c)
18         System.out.println("String.valueOf(\'m\')   "+String.valueOf(\'m\'));
19         // 4.String    valueOf(int i)
20         System.out.println("String.valueOf(96)   "+String.valueOf(96));
21         // 5.String    valueOf(long l)
22         System.out.println("String.valueOf(12345L)   "+String.valueOf(12345L));
23         // 6.String    valueOf(float f)
24         System.out.println("String.valueOf(1.414f)   "+String.valueOf(1.414f));
25         // 7.String    valueOf(double d)
26         System.out.println("String.valueOf(3.14159d)   "+String.valueOf(3.14159d));
27         // 8.String    valueOf(char[] data)
28         System.out.println("String.valueOf(new char[]{\'s\',\'k\',\'y\'})   "+String.valueOf(new char[]{\'s\',\'k\',\'y\'}));
29         // 9.String    valueOf(char[] data, int offset, int count)
30         System.out.println("String.valueOf(new char[]{\'s\',\'k\',\'y\'}, 0, 2)   "+String.valueOf(new char[]{\'s\',\'k\',\'y\'}, 0, 2));
31     }
32 }
View Code

 

String查找特定字符或字符串的API

1 String istr = new String("abcAbcABCabCaBcAbCaBCabc");
2 istr.indexOf((int)\'a\')         = 0//从前往后,找出‘a’第一次出现的位置
3 istr.indexOf((int)\'a\', 5)      = 9//从位置5开始,从前往后,找出‘a’第一次出现的位置
4 istr.lastIndexOf((int)\'a\')     = 21//从后往前,找出‘a’第一次出现的位置
5 istr.lastIndexOf((int)\'a\', 10) = 9//从位置10开始,从后往前,找出‘a’第一次出现的位置
6 istr.indexOf("bc")             = 1//从前往后,找出"bc"第一次出现的位置
7 istr.indexOf("bc", 5)          = 22//从位置5开始,从前往后,找出"bc"第一次出现的位置
8 istr.lastIndexOf("bc")         = 22//从后往前,找出"bc"第一次出现的位置
9 istr.lastIndexOf("bc", 4)      = 4//从位置4开始,从后往前,找出"bc"第一次出现的位置
View Code

 

以上是关于String的主要内容,如果未能解决你的问题,请参考以下文章

如何理解这段代码片段中的两对括号?

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段

面试常用的代码片段

21个常用代码片段

js常用代码片段

java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段