memorandum.
A character string that is String type data cannot be rewritten by itself. Also, since the String type is a reference type, if a different character string is assigned to the initialized String type variable, a new character string will be generated instead of rewriting the original character string, and the variable * * The reference destination is switched **.
Sample.java
String s = "onigiri";
Method | Description | result |
---|---|---|
charAt(2) | Returns the character in the argument. Start is 0th | i |
equals("onigiri") | Compares with the argument string and returns as a boolean value. | true |
intern() | Returns a unique string in the string pool | onigiri |
indexOf('g') | Returns the position where the argument character first appears. Start is 0th | 3 |
length() | Returns the number of strings | 7 |
replace('i','Y') | Replaces the character in the first argument with the character specified in the second argument and returns the resulting string | onYgYrY |
substring(2) | Returns a substring from the position specified by the argument to the end. Start is 0th | igiri |
Main.java
public class Main {
public static void main(String[] args){
String s = "onigiri";
System.out.println("charAt() : " + s.charAt(2));
//Output result "charAt() : i」
System.out.println("indexOf() : "+ s.indexOf('g'));
//Output result "indexOf() : g」
System.out.println("length() : " + s.length());
//Output result "length"() : 7」
System.out.println("replace() : " + s.replace('i','Y'));
//Output result "onYgYrY"
System.out.println("substring() : " + s.substring(2));
//Output result "igiri"
}
}
Like the String class, the StringBuilder class handles strings, but ** it is possible to change the string stored in a variable **. Unlike the String class, you can add or replace characters to a once generated string.
Sample.java
StringBuilder sb = new StringBuilder("ShioMusubi");
Method | Description | result |
---|---|---|
append("LUNCH") | Adds the string specified by the argument to the current string | ShioMusubiLUNCH |
insert(4,"UME") | Inserts the character string specified by the argument before the character at the position specified by the argument. Start is 0th | ShioUMEMusubi |
delete(0,4) | Delete the characters in the position from the first argument to the position immediately before the position of the second argument. Start is 0th | Musubi |
replace(4,sb.length(),"ONIGIRI") | Replaces the character at the position immediately before the position of the first argument to the position of the second argument with the character string specified by the third argument. Start is 0th | ShioONIGIRI |
substring(7) | Returns a substring from the position specified by the argument to the end. Start is 0th | ubi |
Main.java
public class Main {
public static void main(String[] args){
StringBuilder sb1 = new StringBuilder("ShioMusubi");
System.out.println("append() : " + sb1.append("LUNCH"));
//Output result "append"() :ShioMusubiLUNCH」
StringBuilder sb2 = new StringBuilder("ShioMusubi");
System.out.println("insert() : "+ sb2.insert(4,"UME"));
//Output result "insert"() : ShioUMEMusubi」
StringBuilder sb3 = new StringBuilder("ShioMusubi");
System.out.println("delete() : "+ sb3.delete(4,"UME"));
//Output result "delete"() : Musubi」
StringBuilder sb4 = new StringBuilder("ShioMusubi");
System.out.println("replace() : " + sb4.replace(4,sb.length(),"ONIGIRI"));
//Output result "Shio ONIGIRI"
StringBuilder sb5 = new StringBuilder("ShioMusubi");
System.out.println("substring() : " + sb5.substring(7));
//Output result "ubi"
}
}
It's hard to learn this. I would like you to point out any inadequacies, and let me know if it is easy to remember.
Recommended Posts