[Java] Handling of character strings (String class and StringBuilder class)

memorandum.

String class

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 **.

String class methods

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"
  }
}

StringBuilder class

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.

String class methods

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

[Java] Handling of character strings (String class and StringBuilder class)
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] Comparison of String type character strings
[Java] The confusing part of String and StringBuilder
[Java] About String and StringBuilder
Various methods of Java String class
StringBuffer and StringBuilder Class in Java
[Java] Comparison method of character strings and comparison method using regular expressions
[Java] Inheritance and structure of HttpServlet class
I tried to summarize the methods of Java String and StringBuilder
[java] Summary of how to handle character strings
About Java StringBuilder class
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Use of Abstract Class and Interface properly in Java
About full-width ⇔ half-width conversion of character strings in Java
[Java] Practice of exception handling [Exception]
Java inflexible String class substring
[Java] Comparator of Collection class
Java class definition and instantiation
Summary of Java Math class
Advantages and disadvantages of Java
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] Set structure of collection class (about HashSet and TreeSet)
About fastqc of Biocontainers and Java
[Java] Remove whitespace from character strings
Handling of time zones using Java
[Note] Handling of Java decimal point
[Java] Judgment of identity and equivalence
[Algorithm] Descending order of character strings
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
Step-by-step understanding of Java exception handling
[Java] Difference between StringBuffer and StringBuilder
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
[Java] How to use substring to cut out a part of a character string
[Java] How to get to the front of a specific string using the String class
[Java] Check if the character string is composed only of blanks (= Blank)
Display Japanese calendar and days of the week using java8 standard class
[Java] Divide a character string by a specified character
After 3 months of Java and Spring training
[Java] Differences between instance variables and class variables
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Collection and StringBuilder operation method comparison
Proper use of interface and abstract class
[Java] Precautions when comparing character strings with character strings
Java programming (static clauses and "class variables")
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
[Java] Difference between equals and == in a character string that is a reference type
Java string
[Java] How to easily get the longest character string of ArrayList using stream
The story of low-level string comparison in Java
[Java] How to use FileReader class and BufferedReader class
[ev3 × Java] Interface, implementation and inheritance (event handling)
[Delete the first letter of the character string] Ruby
[Java] Personal summary of classes and methods (basic)
[Java] Get the length of the surrogate pair string
[Note] Java: Measures the speed of string concatenation