Write in front:

Hello, everyone. I’m Fdog, from a small city in Inner Mongolia. I’m currently studying in Taizhou. I am very grateful to have such a platform where I can share what I have learned and felt. I like programming, I like code, I like to be a programmer. Study hard, strive for many years later, to give their relatives a better life. QQ/WX: 2506897252 Welcome to exchange.


@[TOC]


1. Obtain the substring

(1) SubString (int beginlndex) This method returns a substring truncated from the specified index position to the end of the string. The syntax is as follows:

String str ="Hello Word";
Strsubstr =str.substring(3);
// The output is lo Word
Copy the code

(2) SubString (int beginIndex, intendIndex) This method returns a substring that begins at an index position in the string and ends at an index position. The syntax is as follows:

String str ="Hello Word";
Strsubstr =str.substring(0.3);
// Output hel
Copy the code

2. Remove whitespace

The trim () method returns a copy of the string, ignoring leading and trailing whitespace.

Strint str ="hello world";/ / STR. Length () value is 12
//str.trim().length() is 11, with Spaces removed
Copy the code

3. String replacement

The replace () method implements the replacement of a specified character or string with a new character or string.

String str = "address"; String newstr = STR. Replace ("a"."A");
// Replace a with a. If a occurs more than once, replace all
Copy the code

4. Determine the beginning and end of the string

1. StartsWith () this method is used to check whether the current string object is prefixed with the specified string. 2. EndsWith () this method is used to check whether the current string object endsWith the specified string.

String num1="22045612"; String num2="21304578";

num1.startsWith("22");/ / is true
num1.endsWith("22");/ / is false
Copy the code

5. Check whether the strings are equal

Equals () str.equals(String otherstr) where STR, otherstr are two String objects to compare.

2. Str.equalsignrecase (String otherSTR) equals is case sensitive and equalsIgnoreCase is case insensitive.

6. Compare two strings in dictionary order

The comparison is based on the Unicode values of the characters in the String, in lexicographical order. If the String is placed before the I parameter String, the result of the comparison is a negative integer followed by a positive integer. If two strings are equal, the result is 0.

String str1="b";
String str2="a";
String str3="c";

str.compareTo(str2)/ / 1
str.compareTo(str2)/ / to 1

Copy the code

7. Case conversion

ToLowerCase () is converted toLowerCase, returning the original string if it has not been converted, or a new one otherwise. ToUpperCase () is capitalized. If the transfer requirements are met, the transfer is skipped.

8. String splitting

(1) Split (String sign) This method splits according to the given delimiter String. Sign is a separator, and regular expressions can also be used. (2) split(String sign, int limit) This method can split a String according to a given delimiter and limit the number of splits.

String str ="192.168.0.1";
// Split according to ".", use escape character "\\."
String[]firstArray = str.

 - [ ] split

("\ \.");
// Split twice according to ".", using the escape character "\\.";
String[]secondArray = str.split("\ \.".2);
// Print the original value
System.out.println("The original value of STR was: ["+str+"]");
// All partitioned values
System.out.println("The value after all segmentation is:");
for(String a : firstArray)
{
System.out.println("["+a+"]");
}
System.out.println();/ / a newline
// Split the value twice
System.out.println("The value after splitting twice is:");
for(String a : secondArray)
{
System.out.println("["+a+"]");
}
System.out.println();/ / a newline
Copy the code

The running results are shown as follows:

That’s it. If you don’t understand the for statement, look at the end of the article.The foreach statement

If there are mistakes, welcome to criticize, welcome to discuss.

Learn to appreciate others: appreciate others talk, will improve our eloquence; Appreciate the generosity of others, will broaden our minds; Appreciating the kindness of others will purify our mind. To appreciate others is to be less critical and more trusting. More enthusiasm, less indifference; Look up more, look down less. A little more appreciation, contradictions and misunderstandings will be less, the distance between people will be closer. = =