Package com. njupt. Demo;
Import java. util. arrays;
Public class demo2 {
Public static void main (string [] ARGs ){
// String STR = "dumb ";
// System. Out. println (STR );
String STR = "I love Liu Yifei ";
String str1 = "ABCDE ";
// Obtain the string length
System. Out. println (Str. Length ());
System. Out. println (str1.length ());
// Obtain characters at a specific position
System. Out. println (Str. charat (1 ));
// Obtain the index of a specific character
System. Out. println (Str. indexof ('ai '));
// Replace a specific character or string
String str2 = Str. Replace ("Liu Yifei", "Liu Shishi ");
System. Out. println (str2 );
System. Out. println (STR );
// Determine whether two strings are equal
String str3 = "liushishi ";
String str4 = "liuyifei ";
String str5 = "liushishi ";
// Trim () removes spaces on both sides of the string
String str6 = str5.trim ();
System. Out. println (str3.equals (str4 ));
System. Out. println (str3.equals (str5 ));
System. Out. println (str3.equals (str6 ));
String str7 = "I love Liu Yifei ";
String str8 = str7.trim ();
System. Out. println (str8 );
String str9 = str7.replace ("","");
System. Out. println (str9 );
// Convert all strings into uppercase letters
String str10 = str5.touppercase ();
System. Out. println (str10 );
// Convert all strings to lowercase letters
String str11 = "liuyifei ";
System. Out. println (str11 );
System. Out. println (str11.tolowercase ());
// Determine whether the string starts with a certain character
System. Out. println (str11.startswith ("L "));
// Determine whether the string has ended with a certain character
System. Out. println (str11.endswith ("i234 "));
// String Truncation
String str12 = "Iloveyou ";
String str13 = str12.substring (); // truncate the string in the [) Interval
System. Out. println (str13 );
// Substring (1) captures the last element by default.
String str14 = "Iloveyou ";
String str15 = str14.substring (1 );
System. Out. println (str15 );
String str16 = "I. Love. Liu. Yi. Fei ";
String [] str17 = str16.split (",");
// The output value of the following line of code is: ljava. Lang. String; @ 11e78461
// The default implementation of the tostring () method in the string class is package name + class name @ hash value.
System. Out. println (str17 );
// The content of the string array to be output. You must call the arrays. tostring () method.
System. Out. println (arrays. tostring (str17 ));
// Split (Rex, limit); Rex is a separator. Limit is the length of the string array after separation.
// Use Rex as the separator from left to right to separate the strings with limit elements.
String [] str18 = str16.split (".", 2 );
System. Out. println ("length of the print character array:" + str18.length );
System. Out. println ("print the string content:" + arrays. tostring (str18 ));
String str19 = "foo: foo1: foo2: foo3: foo4 ";
System. Out. println (arrays. tostring (str19.split (":", 3 )));
// If you use. As the separator, you must use \ for escape. Think. is an escape character
// Otherwise, the result is different from the Expected One.
String str20 = "foo. foo1.foo2. foo3.foo4 ";
System. Out. println (arrays. tostring (str1_split ("\.", 2 )));
// | Escape using \ when used as the separator; otherwise, the desired result will not be obtained,
// Result: [, F, oo | foo1 | foo2 | foo3 | foo4]
String str22 = "foo | foo1 | foo2 | foo3 | foo4 ";
System. Out. println (arrays. tostring (str22.split ("|", 3 )));
// | Use str22.split ("\\|", 3) as the delimiter)
// The running result is: [Foo, foo1, foo2 | foo3 | foo4]
String str23 = "foo | foo1 | foo2 | foo3 | foo4 ";
System. Out. println (arrays. tostring (str23.split ("\\|", 3 )));
// When multiple delimiters are used, str. Split (separator 1 | separator 2) can be written in this way)
// The output result of the following code is: [acount =? , UU =? , N =?]
String str21 = "acount =? And UU =? Or n =? ";
System. Out. println (arrays. tostring (str21.split ("and | or ")));
// Determine whether the character transfer contains a value
String str24 = "I love Liu Yifei ";
System. Out. println (str24.contains ("love"); // true
System. Out. println (str24.contains (""); // true
System. Out. println (str24.contains (""); // false
System. Out. println ("--------------------------");
String str25 = "I love Liu Yifei ";
String str26 = "I love Liu Yifei ";
Boolean OK = str25.contentequals (str26 );
System. Out. println (OK );
String str27 = new string ("I love Liu Yifei ");
String str28 = new string ("I love Liu Yifei ");
Foo foo1 = new Foo ();
Foo foo2 = new Foo ();
// Some people may have questions about the following. Why is it true? Does equals compare the address value ?.
// Sun overwrites the equals method when developing the string so that it compares the content instead of the address.
System. Out. println (str27.equals (str28); // true
System. Out. println (foo1.equals (foo2); // false
System. Out. println (str27.contentequals (str28); // true
}
}
Class Foo {
}