The Java string type is a string class, and the following describes how to manipulate strings.
1. String connection
The string connection uses the "+" symbol. For example, the following examples:
StringnewString("Hello"); StringnewString("World"); " "// Hello World
2. Get the string length
Gets the string length using Str.length (). For example, the following examples:
StringnewString("Hello Java"); // 10
3, gets the index of the specified string
Gets the index of the specified string using Str.indexof (substr), Str.lastindexof (substr), such as the following sample:
String s4 = new String("how are you"); System.out.println(s4.indexOf("o")); // 1 从头開始查找 System.out.println(s4.lastIndexOf("o")); // 9 从尾開始查找
4. Gets the character of the specified index
Gets the character of the specified index using Str.charat (index), such as the following sample:
StringnewString("Hello Java"); System.out.println(s5.charAt(4// o
5. Remove whitespace from string
There are several ways to remove spaces in a string. Use Str.trim () or Str.replaceall (regex, replacement). You can also use the StringTokenizer class to separate strings using spaces. You need to import the package Java.util.StringTokenizer before use, such as the following examples:
String s6 = new String ("Hello Java");String s7 = S6. Trim();//Remove whitespace at beginning and end of stringString S8 = S6. ReplaceAll(" ","");//Replace All spaces in the stringStringTokenizerSt= new StringTokenizer (S6," ");//use spaces to separate stringsStringBuffer sb = new StringBuffer ();while (St. Hasmoretokens()) {SB. Append(St. NextToken());} System. out. println("\""+ S6 +"\""+"length ="+ S6. Length());//"Hello Java" length =System. out. println("\""+ s7 +"\""+"length ="+ S7. Length());//"Hello Java" length = tenSystem. out. println("\""+ S8 +"\""+"length ="+ S8. Length());//"Hellojava" length = 9System. out. println("\""+ SB. toString() +"\""+"length ="+ SB. toString(). Length());//"Hellojava" length = 9
6. Replace string
The replacement string can replace all substrings, and can replace the first substring, such as the following example:
StringnewString("abc abd bcd"); String sr2 = sr.replace("ab""xx"// 替换所有子串 String sr3 = sr.replaceFirst("ab""xx"// 替换第一个字串 // "xxc xxd bcd" // "xxc adb bcd"
7, string sentence, etc.
There are a number of cases, such as string content, whether to ignore uppercase and lowercase, memory address, or inference at the beginning or end of a string, such as the following example:
String se = new String ("Summer");String SE1 = new String ("Summer");String se2 = new String ("Summer");String SE3 = SE;System. out. println(SE = = SE1);//false compared to memory rather than string contentsSystem. out. println(SE = = SE3);//TrueSystem. out. println(SE. Equals(SE1));//True Compare string contentsSystem. out. println(SE. Equals(SE2));//FalseSystem. out. println(SE. Equalsignorecase(SE2));//true ignores uppercase and lowercaseSystem. out. println(SE2. StartsWith("Summer"));//True string startsSystem. out. println(SE2. EndsWith("Cold"));//False end of string
8. String uppercase and lowercase conversions
string uppercase and lowercase conversions such as the following sample:
StringnewString("hello WORLD"); String// hello world 转换为小写 String// HELLO WORLD 转换为大写 " " + scu);
9. String separation
String separators are examples such as the following:
StringnewString("abc,def,g,h"); String[] ss2 = ss.split(","// 以逗号分隔 for (String x: ss2) { "\t"// abc def g h }
10. Formatting strings
There are many forms of string formatting, such as date formatting, time formatting, and conversion, and the use of the date class requires import packet java.util.Date, such as the following example:
Date d = new Date ();System. out. println(d);//Wed Jul 16:00:36 CST 2015 default formatSystem. out. println(String. Format("%tm", d));//72-digit MonthSystem. out. println(String. Format("%th", d));//162-bit 24-hour systemSystem. out. println(String. Format("%x", the));//1006 binary
11, String, StringBuffer, StringBuilder analogy comparison
string constants, immutable objects, when variable content changes, actually generates a new String object, which affects system performance when multiple, frequently changing variable contents. In particular, when the GC starts to work, the program speed becomes very slow.
stringbuffer: String variable. Thread-safe, when variable content is changed, it is actually manipulating the same object, which is more efficient than a string type.
StringBuilder: String variable. Compatible with StringBuffer, it is non-thread safe, assuming it is a single thread. Prefer to use StringBuilder, which is faster than StringBuffer.
Java string Simple use method