简体   繁体   English

有没有办法可以逐步将字符串的某些部分设置为数组? (JAVA)

[英]Is there a way that I can set certain parts of a string into an array incrementally? (Java)

For example, String letters = "fourgooddogsswam"; 例如,String letters =“fourgooddogsswam”;

Is there a way I can scan the string from left to right 4 characters at a time so that I can set(four ourg urgo rgoo good oodd oddo ddog dogs ogss gssw sswa swam) into a string array? 有没有办法我可以一次从左到右扫描4个字符的字符串,以便我可以设置(字符串数组中的四个字符组合)? I tried using loops but I'm having difficulties getting it to work properly. 我尝试使用循环,但我很难让它正常工作。

Thanks! 谢谢!

public static String[] findWordsOfLength(String letters, int wordSize) {
    if(letters == null) {
        return null;
    }

    int size = letters.length();
    int wordMax = size - wordSize + 1;
    if(size < wordMax || wordMax <= 0) {
        return new String[0];
    }

    int j = 0;
    String[] result = new String[wordMax];

    for (int i = 0; i < wordMax; i++) {
        result[j ++] = letters.substring(i, i + wordSize);
    }

    return result;
}

Use a while loop and arraylist like this, 像这样使用while循环和arraylist,

    String hello = "fourgooddogsswam"; 

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

    int i = 0;
    while (i + 4 <= hello.length()) {

        substrings.add(hello.substring(i, i + 4));
        i++;

    }

    for (String s : substrings) {

        System.out.println(s);

    }

If you want to do this without arraylist just make a string array with size YOURSTRING.length() - (WHATEVERSIZE - 1); 如果你想在没有arraylist的情况下这样做,只需创建一个大小为YOURSTRING.length() - (WHATEVERSIZE - 1);的字符串数组YOURSTRING.length() - (WHATEVERSIZE - 1);

Example

    String hello = "fourgooddogsswam"; 

    String[] substrings = new String[hello.length() - 3];

    int i = 0;
    while (i + 4 <= hello.length()) {

        substrings[i] = hello.substring(i, i + 4);
        i++;

    }

    for (String s : substrings) {

        System.out.println(s);

    }

Just another way to do this. 只是另一种方式来做到这一点。

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test {

 public static void main(String args[]) {
  String str = new String("fourgooddogsswam");

  Pattern pattern = Pattern.compile(".{4,4}");
  Matcher matcher = pattern.matcher(str);

  while (matcher.find()) {
   System.out.println(matcher.group(0));
  }
 }
}

Will print: 将打印:

four
good
dogs
swam

PS Yes, we all hate regex... but it does the work ;) PS是的,我们都讨厌正则表达式......但它确实有效;)

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

相关问题 如何在特定索引处将String分成两部分,并将这两部分都保留在Java中? - How can I split a String into two at a certain Index and keep both parts in Java? 在字符串中选择部分数据的最佳方法,其大小会发生变化 - Best way to select parts certain parts of data in a string that changes in size Java:附加的字符串需要删除某些部分 - Java: Appended string need to remove certain parts 循环搜索并显示数组Java的某些部分 - Loop to search and display certain parts of array Java 如何仅将Json字符串的相关部分转换为Set? - How can I convert only the relevant parts of a Json string to a Set? Java从字符串中提取部分的最佳方法 - Java Best way to extract parts from a string 使用SWIG for Java,如何选择性地对巨大的C / C ++头文件的某些部分进行缓存? - Using SWIG for Java, how can I selectively swig certain parts of a huge C/C++ header file? 我想显示我的字符串数组的某个部分而不是其他部分。 字符串数组用于国家、国家代码和每个国家的 iso - I want to display a certain part of my String-array and not other parts. The string array is for countries, the country codes and the iso for each 插入字符串的某些部分 - inserting in certain parts of the string 用Java解析出数组中字符串的一部分 - Parsing out parts of a string in an array in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM