【问题标题】:.split ( ) on a loop? Is this possible? [duplicate].split ( ) 循环?这可能吗? [复制]
【发布时间】:2016-08-14 02:19:46
【问题描述】:

好的,我已经编写了将段落拆分为句子的代码。这是下面。但是现在我想在“split”这个词处拆分句子。但如何?

import java.util.*;
import java.util.regex.*;

public class sub {
    public static void main(String[] args) {
        String test ="As World War split II loomed after 1938, with the Japanese invasion split of China and the aggression of Nazi Germany, Roosevelt gave strong diplomatic  split and financial support to China and the United Kingdom, while remaining split officially neutral";

        String[] sHolder = test.split("[.,?]");
        for (int i = 0; i < sHolder.length; i++){
            sHolder [i] = sHolder[i].replaceAll("\\[a-z]", "");
        }
}

【问题讨论】:

  • 不确定是否应该在, 上拆分句子。可以按空格分割成单词。
  • 好吧,改变你的正则表达式。如果你想在"split"上拆分,那有什么问题?

标签: java regex loops split


【解决方案1】:

在这种情况下,通过使用 split() 并将您的正则表达式分配给您希望它拆分的字符串,即“拆分”。然后我们只需将其分配给String[] 并循环打印出所有内容。

public static void main( String[] args ) throws Exception
{
    String test ="As World War split II loomed after 1938, with the 
                  Japanese invasion split of China and the aggression of
                  Nazi Germany, Roosevelt gave strong diplomatic  split and
                  financial support to China and the United Kingdom, while 
                  remaining split officially neutral";

    String[] splitString = test.split( "split" );

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

【讨论】:

    猜你喜欢
    • 2013-04-15
    • 1970-01-01
    • 2021-02-14
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多