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();
}
}