Java 文字列の前にある空白のみを除去するサンプル

環境
Windows11 pro 64bit
java 19.0.1

構文
対象の文字列.stripLeading( )
文字列の前にある空白のみを除去するには、「stripLeading」関数を使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
String str1 = " h3ello world3 ";
String str2 = " h3ello world3 ";
System.out.println(str1.stripLeading() + "F");
System.out.println(str2.stripLeading() + "F");
}
}
import java.util.*; public class Main { public static void main(String[] args) throws Exception { String str1 = " h3ello world3 "; String str2 = " h3ello world3 "; System.out.println(str1.stripLeading() + "F"); System.out.println(str2.stripLeading() + "F"); } }
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        String str1 = " h3ello world3 ";
        String str2 = " h3ello world3 ";
        
        System.out.println(str1.stripLeading() + "F"); 
        System.out.println(str2.stripLeading() + "F"); 
    }
}

実行結果
h3ello world3 F
h3ello world3 F

IT

Posted by arkgame