--Methods used to process strings
java
equals(); #Whether the contents are equal
equalsIgnoreCase(); #Do not distinguish between large and small letters
length(); #word count
isEmpty(); #Whether it is a character from
contains(); #Whether to include in part
startsWith(); #Is there a keyword first
endsWith(); #Is there a keyword at the end
indexOf(); #What character the keyword appears in from the front
lastIndexOf(); #Search from the back and what character the keyword appears in from the front
charAt(); #Cut out one character
substring(); #Cut out multiple characters
toLowerCase(); #Convert uppercase to lowercase
toUpperCase(); #Convert lowercase letters to uppercase
trim(); #Remove leading and trailing whitespace
--Concatenation of characters
Until now, we learned that strings are concatenated with the "+ operator", but the "+ operator" discards the old instance when concatenating, and creates and stores a new instance with new. In other words, doing a lot of concatenation means creating a lot of new instances, which puts a heavy load on the JVM.
Therefore, you can use StringBuilder to speed up the process. StringBuiler expands the buffer without using new.
java
#How to use
StringBuilder sb = new StringBuilder(); #Assign the chest of drawers of the StringBuilder class to the variable sb.
sb.append("Hello").append("Hello").append("Is"); #It is added to the variable sb of StringBuilder class by append method.
String s = sb.toString(); #The concatenated character string is taken out and assigned to the variable s.
System.out.println(s); #Execution result=> "Hello"
Use the matches () method.
java
❶ Normal characters:Must be an exact match
String s = "Java";
s.matches("Java"); => true #It is an exact match.
s.matches("JavaScript"); => false #Script does not match.
s.matches("java"); => false #First letter(J)Is in lowercase.
❷ Period:Any single letter
"Java".matches("J.va"); => true #period(.)Is any character, so it can be replaced with any character.
❸ Asterisk:Repeat 0 or more times of the previous character
"Jaaaaaaaaava".matches("Ja*va"); => true #The previous letter, that is, "a", can be repeated any number of times.
"That xx019".matches(".*"); => true #Since the character immediately before is a period and any character can be used, it is an instruction to allow all character strings.
❹ Braces:Repeat the specified number of times
"HELLLO".matches("HEL{3}O"); => true #True because it means that the previous character is repeated 3 times.
#How to specify the number of repetitions
#Pattern description:meaning
#{n} :Repeat n times of the previous character
#{n,} :Repeat n or more of the previous character
#{n,m} :Repeat n or more and m or less of the previous character
#? :Repeat 0 or 1 of the previous character
#+ :One or more repetitions of the previous character
❺ Square brackets:Either character
#Square bracket symbol[]If there is a part surrounded by, it means that it is required to apply to any one character in the square brackets.
"Java".matches("Jav[abc]"); => true #It starts with Jav, and if the 4th character applies to any of abc, it's OK.
❻ Hyphen in square brackets:Any character in the specified range
"java".matches("[a-z]{4}"); => true #a~Specify a range of z and compare it to a string that repeats four times.
#Examples of predefined character classes
#Pattern description:meaning
#¥d :Any number([0-9]Same as)
#¥w :Alphabet / numbers / underscore([a-zA-Z_0-9]Same as)
#¥s :Whitespace character(Spaces, tab characters, newline characters, etc.)
❼ Hat(^)And dollar($):Beginning and ending
#The hat represents the beginning and the dollar represents the end.
"Java".matches(^J.*a$); => true #It means a string of any length, starting with J and ending with a.
--split () method: Split string
java
#Comma for one string(,)Or a colon(:)Can be divided with.
String s = "abc,def:ghi";
String[] words = s.split("[,:]");
# []In(,)When(:)を指定するこWhenで、(,)When(:)の部分をsplitメソッドで区切るWhenいう意味になる。
--replaceAll () method: Replace string
java
#You can replace the part of the character string that matches the pattern with another character string.
String s = "abc,def:ghi";
String w = s.replaceAll("[beh]","X"); #[]If there is a character specified in, it will be replaced with X.
System.out.println(w); #Execution result=> aXc,dXf:gXi
--format () method: Assemble a formatted string
java
String.format("%d month%d day%s.",3,26,"Learn java");
#Execution result=>March 26 I learned java.
#The first argument is the template of the character string to be assembled.(It is called a format specification string)。
# %The symbolic part is called a placeholder, and is the place where the specific values specified in the second and subsequent arguments are poured in order.
#Placeholder format
#%Qualification,digit,Mold(Comma for clarity(,)It is separated by, but in reality it does not have to be separated)
#Qualification(comma(,) => 3桁ごとにcommaを入れる; 0 =>Fill free space with 0; - =>Left justified(Numbers); + =>Forced display of sign)
#digit(Specify the number of display digits. n.When specified in m format, it is displayed with n digits in total and m digits after the decimal point.)
#Mold(d =>integer; s =>String; f =>a few; b =>Boolean value)
Recommended Posts