次の文字列があり、その長さが 36 に達したときに、この文字列をいくつかのサブ文字列に分割したい (「,」を区切り文字として)。36 番目の位置で正確に分割されていません。
String message = "This is some(sampletext), and has to be splited properly";
次の2 つの部分文字列として出力を取得したいと考えてい
ます
。
前もって感謝します。
次の文字列があり、その長さが 36 に達したときに、この文字列をいくつかのサブ文字列に分割したい (「,」を区切り文字として)。36 番目の位置で正確に分割されていません。
String message = "This is some(sampletext), and has to be splited properly";
次の2 つの部分文字列として出力を取得したいと考えてい
ます
。
前もって感謝します。
正規表現に基づくソリューション:
String s = "This is some sample text and has to be splited properly";
Pattern splitPattern = Pattern.compile(".{1,15}\\b");
Matcher m = splitPattern.matcher(s);
List<String> stringList = new ArrayList<String>();
while (m.find()) {
stringList.add(m.group(0).trim());
}
更新:trim()は、パターンをスペースで終わるか文字列で終わるように変更することで削除できます。
String s = "This is some sample text and has to be splited properly";
Pattern splitPattern = Pattern.compile("(.{1,15})\\b( |$)");
Matcher m = splitPattern.matcher(s);
List<String> stringList = new ArrayList<String>();
while (m.find()) {
stringList.add(m.group(1));
}
group(1)は、出力としてパターンの最初の部分(。{1,15})のみが必要であることを意味します。
。{1,15}-1から15までの任意の長さの任意の文字( "。")のシーケンス({1,15})
\ b-単語の区切り(単語の前後の非文字)
(| $)-スペースまたは文字列の終わり
さらに、。{1,15}を囲む()を追加したので、グループ全体として使用できます(m.group(1))。目的の結果に応じて、この式を微調整できます。
更新:メッセージの長さが36を超える場合にのみメッセージをコンマで分割する場合は、次の式を試してください。
Pattern splitPattern = Pattern.compile("(.{1,36})\\b(,|$)");
これがきちんとした答えです:
String message = "This is some sample text and has to be splited properly";
String[] temp = message.split("(?<=^.{1,16}) ");
String part1 = message.substring(0, message.length() - temp[temp.length - 1].length() - 1);
String part2 = message.substring(message.length() - temp[temp.length - 1].length());
これは、16 を超える空白のない一連の文字がある場合を除いて、すべての入力で機能するはずです。また、元の文字列にインデックスを付けることで、最小量の余分な文字列を作成します。
public static void main(String[] args) throws IOException
{
String message = "This is some sample text and has to be splited properly";
List<String> result = new ArrayList<String>();
int start = 0;
while (start + 16 < message.length())
{
int end = start + 16;
while (!Character.isWhitespace(message.charAt(end--)));
result.add(message.substring(start, end + 1));
start = end + 2;
}
result.add(message.substring(start));
System.out.println(result);
}
私が考えることができる最善の解決策は、文字列を反復処理する関数を作成することです。この関数では、空白文字を追跡し、16 番目の位置ごとに、最後に検出された空白の位置に基づいて部分文字列をリストに追加できます。部分文字列が見つかったら、最後に見つかった空白から新たに開始します。次に、部分文字列のリストを返すだけです。
上に示したような単純なテキスト(空白で区切られた単語)がある場合は、いつでもStringTokenizerを考えることができます。これがあなたのケースで機能するいくつかの簡単なコードです:
public static void main(String[] args) {
String message = "This is some sample text and has to be splited properly";
while (message.length() > 0) {
String token = "";
StringTokenizer st = new StringTokenizer(message);
while (st.hasMoreTokens()) {
String nt = st.nextToken();
String foo = "";
if (token.length()==0) {
foo = nt;
}
else {
foo = token + " " + nt;
}
if (foo.length() < 16)
token = foo;
else {
System.out.print("'" + token + "' ");
message = message.substring(token.length() + 1, message.length());
break;
}
if (!st.hasMoreTokens()) {
System.out.print("'" + token + "' ");
message = message.substring(token.length(), message.length());
}
}
}
}