简体   繁体   English

重复条件分割的文本区域

[英]text-area-text-to-be-split-with-conditions repeated

I have a text area wherein i have limited the user from entering more that 15 characters in one line as I want to get the free flow text separated into substrings of max limit 15 characters and assign each line an order number. 我有一个文本区域,在该区域中,我限制用户在一行中输入的字符数不能超过15个,因为我想将自由流动文本分成最大不超过15个字符的子字符串,并为每行分配一个订单号。 This is what I was doing in my java class: 这是我在java类中所做的:

int interval = 15;                
items = new ArrayList();
 TextItem item = null;
 for (int i = 0; i < text.length(); i = i + interval) {
  item = new TextItem ();
  item.setOrder(i);

  if (i + interval < text.length()) {
    item.setSubText(text.substring(i, i + interval));
    items.add(item);
  } 
  else {
    item.setSubText(text.substring(i));
    items.add(item);
  }
}

Now it works properly unless the user presses the enter key. 现在,除非用户按Enter键,否则它可以正常工作。 Whenever the user presses the enter key I want to make that line as a new item having only that part as the subText. 每当用户按下Enter键时,我都希望将该行作为仅包含该部分作为subText的新项。

I can check whether my text.substring(i, i + interval ) contains any "\\n" and split till there but the problem is to get the remaining characters after "\\n" till next 15 or till next "\\n" and set proper order and subText. 我可以检查我的text.substring(i, i + interval )是否包含任何"\\n"并拆分到那里,但是问题是要获取"\\n"之后的剩余字符直到下一个15或直到下一个"\\n"并且设置正确的顺序和subText。

You could reverse the order of doing things: First, split the content of your text area at occurences of '\\n'. 您可以颠倒处理的顺序:首先,在出现'\\ n'时拆分文本区域的内容。 Then take each of the resulting strings and split them into parts of maximum length 15. 然后取出每个结果字符串,并将其拆分为最大长度为15的部分。

Edit: I'm not totally sure about what you want to do, but here's what I would do. 编辑:我不太确定您想做什么,但是这就是我要做的。 Note that I'm not a Java programmer, so I might be making false assumptions, and the following is untested. 请注意,我不是Java程序员,因此我可能会做出错误的假设,并且以下内容未经测试。

int interval = 15;                
items = new ArrayList();
lines = text.split("\n");
for (int i = 0; i < lines.length(); i++)
{
  str = lines[i];
  while (str.length() > interval)
  {
    items.add(str.substring(0,interval)); // add the first 15 characters of str to the list
    str = str.substring(interval); // ... and remove them from str
  }
  items.add(str); // add the rest
}

After that, items is an ArrayList of strings that are in the correct order. 之后,items是按正确顺序排列的字符串的ArrayList。 It should be possible to extend this code to whatever additional information you need to store with the actual strings. 应该可以将此代码扩展到需要与实际字符串一起存储的任何其他信息。

Is this what you're trying to do? 这是您要做什么?

public static void main(String[] args)
{
  String str =
  "\n\n123456789ABCDEF123456\n123456789AB\n\n123" +
  "456789ABCDEF123456789AB\n123456789ABCDEF\n";

  List<String> parts = new ArrayList<String>();

  // remove leading line separators
  str = str.replaceFirst("\\A[\r\n]+", "");

  // match up to 15 non-newline characters, but don't
  // match a zero-length string at the end
  Matcher m = Pattern.compile("(?!\\z).{0,15}").matcher(str);
  while (m.find())
  {
    parts.add(m.group());
  }

  for (String s : parts)
  {
    System.out.println(s);
  }
}

result: 结果:

123456789ABCDEF
123456

123456789AB


123456789ABCDEF
123456789AB

123456789ABCDEF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM