LoginSignup
0
0

More than 5 years have passed since last update.

x10で文字列分割

Posted at

x10で文字列を特定のtokenで分割する。

String#splitメソッドを使う。

split_sample.x10
import x10.io.Console;

class Main {

  public static def main( args: Rail[String] ) {

    val s = "1 ./foobar.out 1 2 3 2 1001";
    val ss: Rail[String] = s.split(" ");
    for( x in ss ) {
      Console.ERR.println(x);
    }
  }
}

実装について

StringHelper#split メソッドが呼ばれている。以下、引用。

StringHelper.x10
class StringHelper {
    static def split(delim:String, str:String):Rail[String] {
        if (delim.equals("")) {
            return new Rail[String](str.length(), (i:Long)=>str.substring(i as Int, (i+1) as Int));
        }
        val ans = new GrowableRail[String]();
        var pos:Int = 0n;
        var nextMatch:Int = str.indexOf(delim, pos);
        while (nextMatch != -1n) {
          ans.add(str.substring(pos, nextMatch));
          pos = nextMatch+delim.length();
          nextMatch = str.indexOf(delim, pos);
        }
        if (pos < str.length()) {
            ans.add(str.substring(pos, str.length()));
        }
        return ans.toRail();
    }
}
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