简体   繁体   English

从字符串java拆分文本

[英]Split text from string java

I am having a long text which i want to split in to small sentences.The following is my text. 我有一个长文本,我想分成几个小句子。以下是我的文本。

I tried Count words in a string method? 我尝试在字符串方法中计算字数吗? but there the solution given was to split the string return trim.split("\\\\s+").length; 但是那里给出的解决方案是拆分字符串return trim.split("\\\\s+").length; . In my text i don't have any spaces. 在我的文字中,我没有任何空格。

సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్‌ప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా. సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్ప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా。

I know split() is used to split the string. 我知道split()用于拆分字符串。 But i don't know how to split above text as there is no space or any other regular expression to split with. 但是我不知道如何在文本上方分割,因为没有空格或其他正则表达式可以分割。

The following code works for splitting the text 以下代码用于拆分文本

String string = "1234,56,789,10,1111111,1111112,12";
char[] ch = string.toCharArray();  
int comma_limit = 3;
int comma_count = 0;
for(int i=0;i<ch.length;i++) 
if (ch[i] == ',') {
    comma_count = comma_count + 1;

if (comma_count % comma_limit == 0)
{
ch[i] = '.';
System.out.println(ch);

     }
  }

使用带有逗号分隔符的split方法,它将返回分隔字符串的数组,然后使用length方法,获取其大小

System.out.println(yourString.split(",").length);
static IEnumerable<string> Split(string str, int chunkSize)
{
    return Enumerable.Range(0, str.Length / chunkSize)
        .Select(i => str.Substring(i * chunkSize, chunkSize));
}

You need to check for corner cases. 您需要检查极端情况。

In the split use the "," to split based on the comma character. 在拆分中,请使用“,”根据逗号字符进行拆分。 return trim.split(",").length;

If you want to split it into specific parts use this. 如果要将其拆分为特定部分,请使用此选项。

String text = "SAMPLEs"; // <- this will contain the large text
int numberOfParts = 2; // the number of split parts

int partLength = text.length() / numberOfParts;

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

for (int i = 0; i < numberOfParts; i++) {
    int start = partLength * i;
    int end = start + partLength;
    parts.add(text.substring(start, end));
    if (text.length() - end < partLength) {
        parts.add(text.substring(end, text.length()));
    }
}

for (int i = 0; i < parts.size(); i++) {
    System.out.println("PART " + i + " contains : " + parts.get(i));
}

Result: 结果:

PART 0 contains : SAM
PART 1 contains : PLE
PART 2 contains : s

You can split using the comma character 您可以使用逗号分隔

String text = "సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్\u200Cప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా.";
String[] lines = text.split(",");
for (int i = 0; i < lines.length; i++) {
    System.out.println("SENTANCE  " + i + "  : "+ lines[i]);
}

Use String Builder instead of string 使用字符串生成器代替字符串

StringBuilder sb = new StringBuilder();
sb.append("సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్‌ప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా");

int totalString = sb.toString().splitby(",").lenght();

Here is substring example you have asked. 这是您要求的子字符串示例。

String longText = "సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్\u200Cప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా.";
int longTextLength = longText.length();
int partLength  = (int) longTextLength / 3;
String part1 = longText.substring(0, partLength);
String part2 = longText.substring(partLength, 2*(partLength));
String part3 = longText.substring(2*(partLength), longTextLength);

I am confused by what you are asking. 我对你的要求感到困惑。 Assuming that you are looking to implement some sort of word wrapping, you can do as follows. 假设您要实现某种自动换行,可以执行以下操作。 It may not be the Best way to do it, but it is a way to do it. 这可能不是最好的方法,但它是一种方法。

divideString("This is my sentence! I would like to split this into 3 Strings with about the same length.", 3);

public static void divideString(String raw, int numberOfDivides) {
    int charsPerString = raw.length()/numberOfDivides;
    String[] refined = new String[charsPerString];
    for(int i=1; i < (raw.length()/charsPerString)+1; i++) {
        refined[i] = raw.substring((charsPerString*i)-charsPerString, charsPerString*i);
        System.out.println(refined[i]);
    }
}

Which would output the following: 将输出以下内容:

This is my sentence! I would l
ike to split this into 3 Strin
gs with about the same length.

Check this once you will get 3 Strings inside loop, You can increase split count.. 一旦您将在循环中获得3个字符串,请选中此复选框,您可以增加拆分计数。

    public void splitStr(){
        String str = "";
        String[] split_str = str.split(",");

        int len = split_str.length;

        int split_len = len/3;

        for (int i = 0; i< len; i++){
            String f1 ="";
            if(i == split_len){
                // first string 
                f1 = split_str[i];

                // You will get 3   f1 strings
                split_len += split_len+ i;
            }
        }
    }

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

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