0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Java】文字列を指定文字で分割する

Posted at

指定された正規表現で文字列を分割する時は、splitメソッドを使用します。

split

指定の正規表現で文字列を分割する

split.java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        String str1 = "レタス かぼちゃ きゅうり";
        String str2 = "いちご,りんご,キウイ";
        
        //空白区切り
        String[] split1 = str1.split(" "); 
        for ( int i = 0; i < split1.length; i++ ) { 
          System.out.println(split1[i]); 
        } 
        
        //カンマ区切り
        String[] split2 = str2.split(","); 
        for ( int i = 0; i < split2.length; i++ ) { 
          System.out.println(split2[i]); 
        } 
    }
}
結果
レタス
かぼちゃ
きゅうり
いちご
りんご
キウイ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?