value を含む String フィールドがあります。
String a = "Local/5028@from-queue-bd7f,1";
今、私の必要に応じて、上記の文字列フィールドから抽出された値「5028」が必要です。
/
これにより、文字列がそれぞれと に分割されます@
。
String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.split("[/@]")[1]);
関数を使用String#substring
して値を取得します。パラメータとして開始インデックスと終了インデックスを渡す必要があります。
String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.substring(a.indexOf('/')+1, a.indexOf('@')));
文字列形式が固定されている場合は、次を使用できます。
String a = "Local/5028@from-queue-bd7f,1";
a = a.substring(a.indexOf('/') + 1, a.indexOf('@'));
System.out.println(a);
正規表現の使用:
String a = "Local/5028@from-queue-bd7f,1";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(a);
System.out.println(m.find() + " " + m.group());
String.Split の使用:
String a = "Local/5028@from-queue-bd7f,1";
String[] split = a.split("/");
System.out.println(split[1].split("@")[0]);
フォーマットが一貫していることがわかっている場合は、部分文字列メソッドを使用するか、/ と @ を使用して文字列を分割し、tokens 配列から 2 番目の値を取得できます。
この文字列が常に指定された形式である場合は、これを試すことができます:
String temp=a.split("@")[0];
System.out.println(temp.substring(temp.length()-4,temp.length()));