如何将没有空格的字符串分解为单词

时间:2016-11-07 15:44:43

标签: java string

如果我有一个字符串hellomynameisjohn(我删除了所有空格,数字,标点等等...使用正则表达式)来获取:n

现在我要做的是每个4字符 - 所以说单词的最小长度是4,所以对于我想在空格中添加的每个 public static int countWords(String original, int minLength){ original = original.replaceAll("[^A-Za-z]","").replaceAll("[0-9]", ""); System.out.println(original); System.out.println(original.length()); int count = 0; return count; } 个字符和增量计数。这是我到目前为止所做的。

    for(int i = 0; i<original.length; i++){

    }

4个字符= 1个字。因此,每4个字符添加一个空格并将计数递增1.我不确定如何将此逻辑转换为代码。我正在考虑使用for循环,但这是我得到了多远。

<n:ROOT xmlns:app="http://company/acms/content/DocType" xmlns:n="http://company/acms/content/DocType">
   <Content>
      <Definition>
         <Code>DocType</Code>
         <Version>1</Version>
      </Definition>
      <DocType>
         <Info>
            <DocumentType>DocType</DocumentType>
            <DocumentClass>Other</DocumentClass>
            <CustomerID>12323423</CustomerID>
            <FinancialAccountID>12312312</FinancialAccountID>
            <MSISDN>34534534</MSISDN>
            <CustomerType>Consumer</CustomerType>
            <CustomerSubType>Consumer-Platinum</CustomerSubType>
            <FirstName>Name</FirstName>
            <ServiceType>ServiceType</ServiceType>
            <DocumentSource>BadValueHere</DocumentSource>
            <BoxReferenceNumber>BoxXXX</BoxReferenceNumber>
            <Industry>OTHERS</Industry>
            <CreationDate>2015-01-01</CreationDate>
            <DocumentID>6666</DocumentID>
            <CreatedBy>UBIX</CreatedBy>
            <DocumentOrigin>HD</DocumentOrigin>
            <Notes>Document location on File System: /path/to/doc.TIF</Notes>
            <Resource parseType="none">
               <URL>/path/filename.pdf</URL>
               <FileName>filename.pdf</FileName>
               <MimeType>image/tiff</MimeType>
            </Resource>
            <FileType>tiff</FileType>
         </Info>
         <Permissions>
            <GroupPermissions>
               <GroupName>99</GroupName>
               <Permissions>C</Permissions>
            </GroupPermissions>
            <GroupPermissions>
               <GroupName>30</GroupName>
               <Permissions>W</Permissions>
            </GroupPermissions>
            <GroupPermissions>
               <GroupName>66</GroupName>
               <Permissions>R</Permissions>
            </GroupPermissions>
         </Permissions>
      </DocType>
   </Content>
</n:ROOT>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你的方法只想计算minLength里面有多少个单词?

 public static int countWords(String original, int minLength){

    original = original.replaceAll("[^A-Za-z]","").replaceAll("[0-9]", "");
    System.out.println(original);

    System.out.println(original.length());

    int count = (original.length()+minLength-1) / minLength;

    return count;
}