Summarize how to handle strings in java.
First, the character string type is described, the basic operations of the String type are summarized, and finally, the character string operations using StringBuilder are summarized.
The String type is immutable. That is, when a character string is concatenated or cut out by an operation, the character string itself does not change, but a new character string is created in the storage area. This has the following features.
--The maintainability of the program is improved because no destructive changes occur. --Easy to see the program --Inefficient memory --Limited string operations that can be done with methods
The mutable (variable) StringBuilder class and StringBuffer class solve this inefficiency and difficulty in operation.
StringBuilder and StringBuffer are used in the same way, the only difference is that StringBuffer is thread-safe and StringBuilder is not thread-safe. Thread-safe is a term that means that problems do not occur when multiple threads process a code in parallel.
Therefore, when multi-thread processing is not performed, it is better to use StringBuilder, which is faster than synchronous processing. How to use it will be described later.
Perform basic operations on the following String type character string S.
String str = "a,b,c";
Get the length of the string. The return value is int type.
str.length()
The return value is char type.
str.charAt(int index)
You can combine strings with +.
str += "String type string"
Splits the string with the specified delimiter. If the delimiter is "", it will be split character by character. The return value is a String type array.
str.split("Delimiter")
endIndex can be omitted. Note that if specified, endIndex is not included. The return value is a String type.
str.substring(int beginIndex, int endIndex)
Since the reference value of String type may be different even if it is the same character string, it cannot be compared with == like python. The return value is boolean.
str.equals("String type string")
With IgnoreCase, you can compare by ignoring uppercase and lowercase letters.
str.equalsIgnoreCase("String type string")
Returns a number that indicates how far before the string to be compared is str. That is, it returns a negative value if the string to be compared is after str, a positive value if it is before, and 0 if it matches. The return value is int type.
str.compareTo("String type string")
"a".compareTo("b") //-1
"b".compareTo("a") //1
"a".compareTo("a") //0
Also here, if you add IgnoreCase, you can compare by ignoring uppercase and lowercase letters.
str.compareToIgnoreCase("String type string")
Does it contain the specified string? The return value is boolean.
str.contains("String type string")
If you want to use regular expressions, use the matches method.
str.matches("Regular expressions")
Prefix match. The return value is boolean.
str.startsWith("String type string")
Backward match. The return value is boolean.
str.endsWith("String type string")
Returns the index of the position where the specified substring first appears. When an argument is added, the index of the position where it first appears after that is returned. The return value is int type.
str.indexOf("String type string")
str.indexOf("String type string", int fromIndex)
Similarly, there is also a method called LastindexOf that returns the index of the last occurrence position.
str.lastIndexOf("String type string")
str.lastIndexOf("String type string", int fromIndex)
If the specified string is included, replace all of them. The return value is a String type.
str.replace("Specified character string", "Replacement string")
You can also use regular expressions with replaceAll.
str.replaceAll("Specified character string (regular expression possible)", "Replacement string")
If you want to replace only the first specified character string, use replaceFirst. Like replaceAll, replaceFirst can use regular expressions.
str.replaceFirst("Specified character string (regular expression possible)", "Replacement string")
The return value is a String type.
str.toUpperCase() //Uppercase
str.toLowerCase() //To lowercase
Various types including numbers can be converted to strings with String.valueOf (). The return value is a String type.
String.valueOf(int num)
If a char type array is used as an argument, it will be a character string that connects the arrays.
char[] c = {'a', 'b', 'c'};
String.valueOf(c) //"abc"Is returned
This is convenient when you want to get the number of digits in a number.
int N = 100000;
String.valueOf(N).length() //It turns out to be 6 digits
Build as follows.
StringBuilder sb = new StringBuilder(str)
Can be converted to String type with toString.
sb.toString()
append corresponds to + of String type. The argument may be int type or char type instead of String type.
sb.append("What you want to combine")
If you want to insert it anywhere, use insert.
sb.insert(int index, "What you want to combine")
You can specify the index and delete the substring. Like other methods, it does not include endIndex.
sb.delete(int beginIndex, int endIndex)
You can replace any index character with another with setCharAt.
sb.setCharAt(int index, char c)
//sb.setCharAt(1, 'z')If so, it becomes azc
Reversed character string is obtained by reverse.
sb.reverse()
Converts a substring with index specified to an arbitrary string.
sb.replace(int start, int end, "Replacement string")
sb.length()
sb.charAt(int index)
sb.indexOf("String type string")
sb.indexOf("String type string", int fromIndex)
sb.lastIndexOf("String type string")
sb.lastIndexOf("String type string", int fromIndex)
The operations of String type and StringBuilder class are summarized. Normally, you can use the String type, but if you perform operations many times and it takes a long time to execute, or if you want to perform operations such as inversion, you should use the StringBuilder class.
Also, in competitive programming, it takes time to output a character string many times, so in that case it is better to append to StringBuilder and output it all at once at the end. If you need a line break, append "\ n".
Oracle Help Center Description of String class Description of StringBuilder class
Recommended Posts