Concatenation of strings
String strings are immutable strings, but they can also be spliced, just to create a new object. String string concatenation can use the "+" operator or the concat (string str) method of String. The advantage of the "+" operator is that you can concatenate any type of data stitching into a string, whereas the Concat method can only stitch strings of string type.
Examples are as follows:
1String S1 = "Hello";2 //Connect using the + operator3String s2 = s1 + ""; 4String s3 = s2 + "World"; 5 System.out.println (S3);6 7String S4 = "Hello";8 //join with + operator, support + = assignment operator9S4 + = ""; TenS4 + = "World"; One System.out.println (S4); A -String S5 = "Hello"; - //Connect using the Concat method theS5 = S5.concat (""). Concat ("World")); - System.out.println (S5); - - intAge = 18; +String s6= "I ' m" + Age + "years old."; - System.out.println (S6); + A CharScore = ' A '; atString s7= "His grade is" +score; - System.out.println (S7); - -Java.util.Date now =Newjava.util.Date (); - //object stitching automatically calls the ToString () method -String s8= "Date:" +Now ; inSystem.out.println (S8);
Output Result:
Hello Worldhello worldhello worldi' m years old. 16:25:40 CST 2017
Lines 3rd and 4 of the preceding code use the + operator for concatenation of strings, resulting in three objects. Lines 9th and 10 of the code use the + = assignment operator, which is essentially the + operator for stitching.
The code line 15th uses the concat method for stitching, the complete definition of the method is as follows:
Public String concat (String str)
Its parameters and return values are string, so line 15th of the code can call the method consecutively to stitch multiple strings.
Lines 19th and 23rd of the code use the + operator to stitch strings to other types of data. Line 28th of the code is that the object can be stitched together, all the objects in Java have a ToString () method, which can convert the object to a string, the stitching process calls the object's ToString () method, converting the object to a string and then stitching. The Java.util.Date class in line 26th of the code is the date class provided by Java SE.
String Lookup
Finding a character or string from a given string is a common operation. The IndexOf and LastIndexOf methods are available in the string class, and the method return value is the location of the character or string that is being searched, and returns 1 if it is not found. The following are other common overloaded methods:
int indexOf (int ch): Searches backward for the character ch, returning the index at which the character Ch was found the first time.
int indexOf (int ch, int fromIndex): Starts from the specified index and searches for the character ch, returning the index where the character ch is located the first time it is found.
int indexOf (String str): Searches backwards for the string str, returning the index where the string was found the first time.
int indexOf (string str, int fromIndex): Starts from the specified index and searches for the string str, returning the index where the string was found the first time.
int lastIndexOf (int ch): Searches for the character Ch from the back forward, returning the index at which the character Ch was found the first time.
int lastIndexOf (int ch, int fromIndex): Searches for the character Ch from the beginning of the specified index, and returns the index where the character ch is located the first time it is found.
int lastIndexOf (String str): Searches forward from the string str, returning the index where the string was found the first time.
int lastIndexOf (string str, int fromIndex): Searches the string str from the beginning of the specified index, and returns the index where the string was found the first time.
Examples are as follows:
String sourcestr = "There is a string accessing example.";//Get string lengthintLen =sourcestr.length ();//gets the character of index position 16Charch = Sourcestr.charat (16);//finding characters and substringsintFirstChar1 = Sourcestr.indexof (' R '));intLastChar1 = Sourcestr.lastindexof (' R '));intFIRSTSTR1 = Sourcestr.indexof ("ing");intLASTSTR1 = Sourcestr.lastindexof ("ing");intFIRSTCHAR2 = Sourcestr.indexof (' e ', 15);intLASTCHAR2 = Sourcestr.lastindexof (' e ', 15);intFIRSTSTR2 = Sourcestr.indexof ("ing", 5);intLASTSTR2 = Sourcestr.lastindexof ("ing", 5); System.out.println ("Original string:" +sourcestr); System.out.println ("String Length:" +Len); System.out.println ("Index 16 characters:" +ch); System.out.println ("Search for R characters from the front and find the index for the first time:" +firstChar1); System.out.println ("Search for R characters from the back, and find the index for the first time:" +lastChar1); System.out.println ("Search for the ING string, first find its index:" +firstStr1); System.out.println ("Search for the ING string from the back forward, first find its index:" +lastStr1); System.out.println ("Start with an index of 15, search for the e character from the beginning, and find the index for the first time:" +firstChar2); System.out.println ("Start with an index of 15, search for the e character from the back, and find the index for the first time:" +lastChar2); System.out.println ("Starting at index 5, searching for the ING string, the first time to find its index:" +firstStr2); System.out.println ("Starting at index 5, searching for the ING string from behind, the first time to find its index:" + LASTSTR2);
Output Result:
Original string: There is a string accessing example. String length: The character of index 16: G search for R characters from the go, first find it in index:3 search for r characters from backward forward, Find it for the first time index:search for the ing string from the go, first find it in index:the first time to search for the ING string, to find its index:the index is 15 position start, Search for the e character, the first time to find its index: 15 starting from the index, searching the e character from the back, and finding it for the first time. Index:4 start with the index of 5, search for the ING string, and find its index for the first time: start with index 5, search for ing string from backward forward, first find it in index:-1
The SOUCRESTR string index is as follows:
string comparison
Strings are also common operations, including comparing equality, comparing size, comparing prefixes and suffixes, and so on.
Compare equal
String provides methods for comparing string equality:
Boolean equals (Object AnObject): Compares the contents of two strings for equality.
Boolean equalsignorecase (String anotherstring): Similar to the Equals method, just ignoring case.
Compare size
Sometimes you need to know not only the equality, but also the size, the size of the string provided by the method:
int CompareTo (string anotherstring): Compares two strings in a dictionary order. Returns a value of 0 if the argument string equals this string, or a value less than 0 if the string is less than the string argument, or a value greater than 0 if the string is larger than the string argument.
int comparetoignorecase (String str): Similar to CompareTo, just ignoring the case.
Compare prefixes and suffixes
Examples are as follows:
1String S1 =NewString ("Hello");2String s2 =NewString ("Hello");3 //compares whether a string is the same reference4System.out.println ("S1 = = S2:" + (S1 = =s2));5 //Compare string contents for equality6System.out.println ("S1.equals (S2):" +(s1.equals (S2)));7 8String s3 = "HELlo";9 //Ignore case to compare string contents for equalityTenSystem.out.println ("S1.equalsignorecase (S3):" +(S1.equalsignorecase (S3))); One A //Compare Size -String S4 = "Java"; -String S5 = "Swift"; the //Compare string size S4 > S5 -System.out.println ("S4.compareto (S5):" +(S4.compareto (S5))); - //Ignore case comparison string size S4 < S5 -System.out.println ("S4.comparetoignorecase (S5):" +(S4.comparetoignorecase (S5))); + - //determine the file name in the folder +String[] Docfolder = {"Java.docx", "Javabean.docx", "objecitve-c.xlsx", "Swift.docx" }; A intWorddoccount = 0; at //find the number of Word documents in a folder - for(String doc:docfolder) { - //go to the front and back space -Doc =Doc.trim (); - //compare suffixes with a. docx string - if(Doc.endswith (". docx")) { inworddoccount++; - } to } +System.out.println ("The number of Word documents in the folder is:" +worddoccount); - the intJavadoccount = 0; * //find the number of Java-related documents in a folder $ for(String doc:docfolder) {Panax Notoginseng //go to the front and back space -Doc =Doc.trim (); the //turn all characters into lowercase +Doc =doc.tolowercase (); A //compare prefixes with Java strings the if(Doc.startswith ("Java")) { +javadoccount++; - } $ } $System.out.println ("The number of Java related documents in the folder is:" + javadoccount);
Output Result:
Falsetrueif thenumber of Java-related documents in the 9 3 folder is: 2
The CompareTo method in line 16th of the preceding code compares two strings in dictionary order, the S4.compareto (S5) expression returns a result greater than 0, stating that S4 is greater than S5, the character is in the dictionary order in fact its Unicode encoding, first comparing the first character of the two string J and S , J's Unicode encoding is 106,s Unicode encoding is 83, so you can draw a conclusion S4 > S5.
Line 18th of the code is ignoring case, either all as lowercase, or currently in all uppercase letters, regardless of which comparison results are the same S4 < S5.
The Code line 26th trim () method can remove whitespace before and after the string. The line 40th toLowerCase () method converts this string to lowercase strings, and a similar method has the toLowerCase () method to convert all strings to lowercase strings.
String interception
The main two common string interception methods in Java are as follows:
string substring (int beginindex): A substring that is truncated from the specified index beginindex until the end of the string.
String substring (int beginindex, int endIndex): The character that is intercepted from the specified index beginindex until the index endIndex-1, including the character indexed to Beginindex, It does not include characters that are indexed to endindex.
Examples are as follows:
1String sourcestr = "There is a string accessing example.";2 //Intercept example. substring3String subStr1 = sourcestr.substring (28); 4 //truncate string substring5String subStr2 = sourcestr.substring (11, 17); 6System.out.printf ("SUBSTR1 =%s%n", SUBSTR1);7System.out.printf ("SUBSTR2 =%s%n", SUBSTR2);8 9 //splitting a string using the Split methodTenSYSTEM.OUT.PRINTLN ("-----Using the Split method-----"); Onestring[] Array = Sourcestr.split (""); A for(String str:array) { - System.out.println (str); -}
Output Result:
SUBSTR1 == string----------thereisastringaccessingexample Using the Split method.
The above Sourcestr string index is as shown. Line 3rd of the code is to intercept the example. Substring, the e-character index visible by the index map is 28, from the index 28 character intercept until the end of Sourcestr. Line 5th of the code is to intercept the string substring, which is visible by the graph, the S character index is the 11,g character index is the 16,endindex parameter should be 17.
In addition, string partitioning method is provided, see Code line 11th split ("") method, parameter is a split string, return value string[].
[Java Learning Basics] Basic manipulation of string strings