This question already has an answer here:
这个问题在这里已有答案:
- Split string to equal length substrings in Java 15 answers
- 在Java 15答案中将字符串拆分为相等长度的子字符串
I want to split a string after a certain length.
我想在一定长度后分割一个字符串。
Let's say we have a string of "message"
假设我们有一串“消息”
Who Framed Roger Rabbit
Split like this :
像这样拆分:
"Who Framed" " Roger Rab" "bit"
And I want to split when the "message" variable is more than 10.
当“消息”变量超过10时,我想分开。
my current split code :
我目前的拆分代码:
private void sendMessage(String message){
// some other code ..
String dtype = "D";
int length = message.length();
String[] result = message.split("(?>10)");
for (int x=0; x < result.length; x++)
{
System.out.println(dtype + "-" + length + "-" + result[x]); // this will also display the strd string
}
// some other code ..
}
4 个解决方案
#1
16
I wouldn't use String.split
for this at all:
我根本不会使用String.split:
String message = "Who Framed Roger Rabbit";
for (int i = 0; i < message.length(); i += 10) {
System.out.println(message.substring(i, Math.min(i + 10, message.length()));
}
Addition 2018/5/8:
增加2018/5/8:
If you are simply printing the parts of the string, there is a more efficient option, in that it avoids creating the substrings explicitly:
如果您只是打印字符串的部分,那么有一个更有效的选项,因为它避免显式创建子字符串:
PrintWriter w = new PrintWriter(System.out);
for (int i = 0; i < message.length(); i += 10) {
w.write(message, i, Math.min(i + 10, message.length());
w.write(System.lineSeparator());
}
w.flush();
#2
3
I think Andy's solution would be the best in this case, but if you wanted to use a regex and split you could do
我认为Andy的解决方案在这种情况下是最好的,但是如果你想使用正则表达式并且你可以做分裂
"Who Framed Roger Rabbit ".split("(?<=\\G.{10})");
#3
0
This will work for you. Works for any length of message.
这对你有用。适用于任何长度的消息。
public static void main(String[] args) {
String message = "Who Framed Roger Rab bit";
if (message.length() > 10) {
Pattern p = Pattern.compile(".{10}|.{1,}$");
Matcher m = p.matcher(message);
while (m.find()) {
System.out.println(m.group());
}
}
}
O/ P :
O / P:
Who Framed
Roger Rab
bit
#4
0
You could use a regex find, rather than a split something like this: [\w ]{0,10}
您可以使用正则表达式查找,而不是像这样的拆分:[\ w] {0,10}
Pattern p = Pattern.compile("[\\w ]{0,10}");
Matcher m = p.matcher("who framed roger rabbit");
while (m.find()) {
System.out.println(m.group());
}
#1
16
I wouldn't use String.split
for this at all:
我根本不会使用String.split:
String message = "Who Framed Roger Rabbit";
for (int i = 0; i < message.length(); i += 10) {
System.out.println(message.substring(i, Math.min(i + 10, message.length()));
}
Addition 2018/5/8:
增加2018/5/8:
If you are simply printing the parts of the string, there is a more efficient option, in that it avoids creating the substrings explicitly:
如果您只是打印字符串的部分,那么有一个更有效的选项,因为它避免显式创建子字符串:
PrintWriter w = new PrintWriter(System.out);
for (int i = 0; i < message.length(); i += 10) {
w.write(message, i, Math.min(i + 10, message.length());
w.write(System.lineSeparator());
}
w.flush();
#2
3
I think Andy's solution would be the best in this case, but if you wanted to use a regex and split you could do
我认为Andy的解决方案在这种情况下是最好的,但是如果你想使用正则表达式并且你可以做分裂
"Who Framed Roger Rabbit ".split("(?<=\\G.{10})");
#3
0
This will work for you. Works for any length of message.
这对你有用。适用于任何长度的消息。
public static void main(String[] args) {
String message = "Who Framed Roger Rab bit";
if (message.length() > 10) {
Pattern p = Pattern.compile(".{10}|.{1,}$");
Matcher m = p.matcher(message);
while (m.find()) {
System.out.println(m.group());
}
}
}
O/ P :
O / P:
Who Framed
Roger Rab
bit
#4
0
You could use a regex find, rather than a split something like this: [\w ]{0,10}
您可以使用正则表达式查找,而不是像这样的拆分:[\ w] {0,10}
Pattern p = Pattern.compile("[\\w ]{0,10}");
Matcher m = p.matcher("who framed roger rabbit");
while (m.find()) {
System.out.println(m.group());
}