MENU

Summary of Basic String Manipulation in Java

TOC

Split string - split

Split a string with a specific string (delimiter).

sample code

public static void splitSample() {
    String line = "apple,mandarin orange,banana";
    String[] fruits = line.split(",");
    for (String fruit : fruits) {
        System.out.println(fruit);
    }
}

Output Results:

apple
mandarin orange
rape (Brassica rapa var. amplexicaulu)

Basic String Concatenation

Strings can be combined with the "+" operator.

sample code

public static void plusSample() {
    String s1 = "hello ";
    String s2 = "world!";
    System.out.println(s1 + s2);
}

Output Results:

hello world!


Combine strings with delimiters -. join

Combines strings with a specific string (delimiter) (available in java8 or later)

sample code

public static void joinSample() {
    String line = String.join(","", "apple", "mandarin", "banana");
    System.out.println(line);
}

join can be joined with string arrays in the same way.

public static void joinArraySample() {
    String[] fruits = {"apple", "mandarin", "banana"};
    String line = String.join(","", fruits);
    System.out.println(line); //result is the same
}

Output Results:

Apples, oranges, bananas

Cut out a string from a specified range of characters. substring

Crop a portion of a string by specifying the start and end points of the cutout.

sample code

public static void substringSample() {
    String str = "hello world!";
    System.out.println(str.substring(0, 5));
    System.out.println(str.substring(2, 9));
    System.out.println(str.substring(6)); //Only the starting point can be specified
}

Output Results:

hello
llo wor
world!

前後の空白を削除する – trim

Remove half-width spaces, line breaks, and tabs before and after a string.
Blanks and double-byte spaces in the middle of the string are not removed.

sample code

public static void trimSample() {
    String str = " hello world!";
    System.out.println(str.trim());
}

Output Results:

hello world!

文字列の一部を別の文字列で置き換える – replace

Replaces part of a string with the specified string. replace replaces all matching strings.

sample code

public static void replaceSample() {
    String str = "hello world!";
    System.out.println(str.replace("l", "x"));
}

Output Results:

he x x o wor xd!

replaceFirst replaces only the first matching string.

sample code

public static void replaceFirstSample() {
    String str = "hello world!";
    System.out.println(str.replaceFirst("l", "x"));
}

Output Results:

he x lo world!

Returns the number of characters in a string - - Returns the number of characters in a string length

Returns the number of characters in a string; String.length() simply returns the number of characters.

sample code

public static void lengthSample() {
    String str = "10-11, △△-cho, □□ city, 00 prefecture";
    System.out.println(str.length());
}

Output Results:

14

If you want to know the number of bytes, use String.getBytes().length; since the standard character encoding from Java8 is UTF8, each double-byte character is 3 bytes.

sample code

public static void byteLengthSample() {
    String str = "10-11, △△-cho, □□ city, 00 prefecture";
    System.out.println(str.getBytes().length);
}

Output Results:

32
TOC