Java common string method summary
- 2020-06-23 00:24:45
- OfStack
The following is a brief summary of the code for string manipulation. Most are String class operation method, need friends can refer to below
public class StudyString {
public static void main(String[] ergs){
// String declaration and assignment
String name = " Cai Yufei ";
String hisname = new String (" Xiao Ming ");
System.out.println(name+" and "+hisname+" It is a good friend ");
// String basic operation
// Gets the length of the string
// String name .length() Returns the number of characters
String hello = "hello world!";
int length = hello.length();
System.out.println(hello+" Is the length of the "+length);
// String concatenation
//String Class provides the concat() methods
// string 1.concat( string 2) The return value is 1 A string
String twoname = name.concat(hisname);
System.out.println(twoname);
// String comparison
//String To provide the equals() Method with a return value of boolean Type. Two strings in each character are complete 1 For the cause turn. Otherwise, for false
// string 1.equals( string 2)
String str1 = "fuck";
String str2 = "FUCK";
if (str1.equals(str2))
System.out.println(" The same ");
else
System.out.println(" different ");
//String Also provides equalsIgnoreCase() Method, which differs from the above one in that it is case-insensitive. The return value is the same boolean type
// string 1.equalsIgnoreCase( string 2)
if(str1.equalsIgnoreCase(str2))
System.out.println(" The same ");
else
System.out.println(" different ");
// String interception
// Extract from a string 1 Part as a new string, String Class provides the substring To implement the
// string .substring ( The starting position ); or string .substring ( The starting position , End position );
// The first 1 The species is from the beginning to the end, no 2 Species from the beginning to the end .
String my ="my name is caiyufei,I love Java and Python.";
String love =my.substring(20);
String myname = my.substring(11, 19);
System.out.println(love);
System.out.println(myname);
// String lookup
// in 1 Find another in a string 1 A string of, String Class provides a indexOf Method to implement
// string 1.indexOf( string 2) or string 1.indexOf( string 2, The starting position )
int lovenum = my.indexOf(love);
int mynamenum = my.indexOf(myname);
System.out.println(lovenum);
System.out.println(mynamenum);
// String substitution
// with 1 Three new characters to replace all the characters specified in the string String Class provides a replace Method to implement this substitution
// string 1.replance( Substituted character , Substitution characters )
char I_ = 'I';
char m_ = 'M';
System.out.println(love.replace(I_, m_)); //M love Java and Python.
// String and character arrays
// Converts an array of characters directly to a string as an argument to the constructor
char [] helloArray = {'h','e','l','l','o'};
String helloString = new String (helloArray);
System.out.println(helloString);
// Converts a string to an array of characters
//toCharArray() methods
char [] Array = helloString.toCharArray();
for (int i = 0;i<Array.length;i++)
System.out.println(Array[i]);
// Other methods
// Chinese and English characters are converted to lowercase characters
String eng = "I Love English";
String eng_1 = eng.toLowerCase();
System.out.println(eng_1);
// The Chinese and English characters of the string are capitalized
String eng_2 = eng.toUpperCase();
System.out.println(eng_2);
// Returns the character at the specified index
char en = eng.charAt(5);
System.out.println(en);//e
// Compare strings, return int
int num_1 = eng.compareTo(eng_1);
System.out.println(num_1);
// Returns the first 1 The position of the substring found, or none -1
String loves = "Love";
int lovenum1 = eng.indexOf(loves);
System.out.println(lovenum1); //2
// Removes Spaces before and after the string
String world = " I love my world ";
System.out.println(world);
world = world.trim();
System.out.println(world);
// judge suffix Is the beginning of a string
String suffix = "world";
if (world.startsWith(suffix))
System.out.println("world Is the string world The beginning of the ");
else
System.out.println("world Not a string world The beginning of the ");
// judge suffix Is the end of the string
if (world.endsWith(suffix))
System.out.println("world Is the string world At the end of the ");
else
System.out.println("world Not a string world At the end of the ");
//String buffer class
// Greatly improves the processing speed of strings, but the disadvantage is that it takes up a lot of memory. Used when dealing with extremely large Numbers of characters
StringBuffer sb = new StringBuffer("StringBuffer beautifer good");
System.out.println(sb);
// Add parameters to StringBuffer In the object
sb.append(",");
sb.append("I love StringBuffer!");
System.out.println(sb);
// delete StringBuffer Specifies a character or sequence of characters in the
sb.deleteCharAt(0);// Deleting the location specified 1 A character
System.out.println(sb);
sb.delete(0, 12);// Delete from one place to another
System.out.println(sb);
//
}
}
The above code learning friends can refer to