繁体   English   中英

将字符串分成java中的子字符串

[英]Divide string into substrings in java

我有一个字符串1122333344555566778888我需要对其进行子串,因此得到[11, 22, 3333, 44, 5555, 66, 77, 8888] 1122333344555566778888 [11, 22, 3333, 44, 5555, 66, 77, 8888]是否可以以漂亮的方式进行,或者我需要对其进行硬编码和8时间使用string.substring(beginning, ending)函数然后放入array

编辑:字符串不仅可以包含重复的数字。 AB CG HERD KJ 98HQ 0K 1E OOQW也是例子!

使用模式: ((\\d)\\2*)

String input = "1122333344555566778888";
Pattern p = Pattern.compile("((\\d)\\2*)");
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println("Found " + m.group(1));
}

生产:

11
22
3333
44
5555
66
77
8888

编辑:如果它的数字以及空格和字母使用模式(([\\d\\w\\s])\\2*)

您可以使用重复字符的正则表达式:

String input = "1122333344555566778888";
String regex = "(\\w)\\1+";

Matcher m = Pattern.compile(regex).matcher(input);
String[] substrings = new String[input.length()];

int index = 0;

while (m.find())
    substrings[index++] = m.group();

for (int i = 0; i < index; i++)
    System.out.println(substrings[i]);

输出:

11
22
3333
44
5555
66
77
8888

重要的提示:

substrings数组包含空条目,因为它的长度等于输入字符串的长度。 如果您的字符串包含非重复的连续字符,则此数组可能没有空条目。 观察NullPointerException substrings

这个字符串中没有分隔符可以使用.split(),如果你想要有一个子字符串之间的分隔符,如11-22-3333- ...等,它将很容易使用

String[] splits = asseltClasses.split("-");

基于BlueBullet的......

import java.util.regex.*;
import java.util.*;
public class MyTest {

    public static void main(String[] args) {

        String input = "1122333344555566778888";
        String regex = "(\\w)\\1+";

        Matcher m = Pattern.compile(regex).matcher(input);

        List<String> l = new ArrayList<String>();
        while (m.find()) l.add(m.group());

        System.out.println(Arrays.toString(l.toArray()));
    }   
}

输出:

[11, 22, 3333, 44, 5555, 66, 77, 8888]

这够漂亮吗? 这只是一条线......

String parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0");

这是一个测试:

public static void main(String[] args) {
    String input = "1122333344555566778888";
    String[] parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0");
    System.out.println(Arrays.toString(parts));
}

输出:

[11, 22, 3333, 44, 5555, 66, 77, 8888]

请注意,这个解决方案有一个非常小的障碍 - 在调用replaceAll() $1之后的字符一定不能输入。 我选择空字符'\\0' (即十六进制零)是相当安全的。

这就是我要做的:给定一个未排序的字符串“ABCABCABC”,您可以通过将其转换为Char []数组进行排序,然后使用Arrays.sort()将其转换为AAABBBCCC。

    public String[] sortThis(String inputData) {
    String input = "ABCABCABC"; //make this whatever you want (or set to inputData)
    String[] temp = new String[input.length()];
    for (int i = 0; i < input.length();i++) //initialize the array, or it prints "null"
        temp[i] = "";
    int index = 0;
    char[] info = input.toCharArray();
    Arrays.sort(info);

    for (int i = 0; i < input.length(); i++) { // fill the temp array
        temp[index] += info[i];
        if(i+1 < input.length())
            if(i < input.length() && info[i] != info[i+1])
                index++;
    }

    String[] answer = new String[index+1]; 
    for(int i = 0; i < index+1; i++) // shorten the array
        answer[i] = temp[i];

    return answer;
    }

输出:

    [AAA, BBB, CCC]

也许你想用这个:

var str="1122333344555566778888"; //whatever
b=0;outpt="";
while(b<=18){
if(b==4|b==10|b==18){e=4;p=4}else{e=2;p=2}
outpt+=str.substr(b,e)+", ";b+=p;}
alert(outpt);

输出:

11, 22, 3333, 44, 5555, 66, 77, 8888, 

暂无
暂无

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

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