Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何根据输入将字符串拆分为多行?_Java_String_Split - Fatal编程技术网

Java-如何根据输入将字符串拆分为多行?

Java-如何根据输入将字符串拆分为多行?,java,string,split,Java,String,Split,我是Java新手,希望根据输入的最大长度将用户输入的消息拆分为多行。我该怎么重复呢?以下是我到目前为止的情况: int rem = m - maxlength; System.out.println(message.substring(0, message.length() - rem)); System.out.println(message.substring(message.length() - rem)); 正则表达式版本(最简单): 没有正则表达式: import java.uti

我是Java新手,希望根据输入的最大长度将用户输入的消息拆分为多行。我该怎么重复呢?以下是我到目前为止的情况:

int rem = m - maxlength;
System.out.println(message.substring(0, message.length() - rem));
System.out.println(message.substring(message.length() - rem)); 
正则表达式版本(最简单):

没有正则表达式:

import java.util.List;
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
      String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
      int lineMaxLength = 10;
      List<String> lines = new ArrayList<>();
      int length = text.length();
      StringBuilder s = new StringBuilder();
      for(int i = 0; i < length; i++){
          if (i % lineMaxLength == 0){
              if(i != 0){
                  lines.add(s.toString());
              }
              s = new StringBuilder();
          }
          s.append(text.charAt(i));
      }
  int linesLength = lines.size();
  for(int i= 0; i < linesLength; i++){
      System.out.println(lines.get(i));
  }
    }
}

这类工作。我选择了一个非常短的“行”长度为10个字符,您可能应该增加这个长度。但它展示了如何在没有太多代码的情况下实现这一点。将正则表达式中的“10”更改为不同的数字以增加行长度

(我修改了代码,使它更加明显,该方法不在中间拆分单词,但总是在空白处分割)。


像这样使用WordIterator。BreakIterator类对区域设置敏感。你可以使用不同的语言

public static void main(String[] args) {
    String msg = "This is a long message. Message is too long. Long is the message";
    int widthLimit = 15;
    printWithLimitedWidth(msg, widthLimit);
}

static void printWithLimitedWidth(String s, int limit) {
    BreakIterator br = BreakIterator.getWordInstance(); //you can get locale specific instance to handle different languages.
    br.setText(s);
    int currLenght = 0;
    int start = br.first();
    int end = br.next();

    while (end != BreakIterator.DONE) {
       String word = s.substring(start,end);
       currLenght += word.length(); 
       if (currLenght <= limit) {
          System.out.print(word);
       } else {
           currLenght = 0;
           System.out.print("\n"+word);
       }
       start = end;
       end = br.next();
    }
}
这是一个示例代码。根据需要处理空白和其他特殊字符。 有关更多信息,请参阅
int maxlength=10;
字符串消息=“1234567890abcdefghijklmnopqrstuvxyz”;

字符串transformedMessage=message.replaceAll(“(?您可以添加一些示例数据来帮助解释您试图在这里实现的目标。您可以使用正则表达式来实现这一点,尽管实际的正则表达式有点复杂,但我需要花一点时间才能完成。使用BreakIterator类来处理多种语言的问题。BreakIterator类对语言环境敏感。
import java.util.List;
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
      String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
      int lineMaxLength = 10;
      List<String> lines = new ArrayList<>();
      int length = text.length();
      StringBuilder s = new StringBuilder();
      for(int i = 0; i < length; i++){
          if (i % lineMaxLength == 0){
              if(i != 0){
                  lines.add(s.toString());
              }
              s = new StringBuilder();
          }
          s.append(text.charAt(i));
      }
  int linesLength = lines.size();
  for(int i= 0; i < linesLength; i++){
      System.out.println(lines.get(i));
  }
    }
}
Lorem Ipsu
m is simpl
y dummy te
xt of the 
printing a
nd typeset
ting indus
try. Lorem
 Ipsum has
 been the 
industry's
 standard 
dummy text
 ever sinc
e the 1500
s, when an
 unknown p
rinter too
k a galley
 of type a
nd scrambl
ed it to m
ake a type
 specimen 
   public static void main( String[] args ) {
      String s = "This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.  ";
      String regex = ".{1,10}\\s";
      Matcher m = Pattern.compile( regex ).matcher( s );
      ArrayList<String> lines = new ArrayList<>();
      while( m.find()  ) {
         lines.add(  m.group() );
      }
      System.out.println( String.join( "\n", lines ) );
   }
This is a 
test. This 
is a test. 
This is a 
test. This 
is a test. 
This is a 
test. This 
is a test. 
BUILD SUCCESSFUL (total time: 0 seconds)

       
public static void main(String[] args) {
    String msg = "This is a long message. Message is too long. Long is the message";
    int widthLimit = 15;
    printWithLimitedWidth(msg, widthLimit);
}

static void printWithLimitedWidth(String s, int limit) {
    BreakIterator br = BreakIterator.getWordInstance(); //you can get locale specific instance to handle different languages.
    br.setText(s);
    int currLenght = 0;
    int start = br.first();
    int end = br.next();

    while (end != BreakIterator.DONE) {
       String word = s.substring(start,end);
       currLenght += word.length(); 
       if (currLenght <= limit) {
          System.out.print(word);
       } else {
           currLenght = 0;
           System.out.print("\n"+word);
       }
       start = end;
       end = br.next();
    }
}
This is a long 
message. Message is 
too long. Long is 
the message
int maxlength = 10;
String message = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String transformedMessage = message.replaceAll("(?<=\\G.{" + maxlength + "})", "\n");
System.out.println(transformedMessage);
/*Output
1234567890
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ
*/