Split string into substrings in java

I have a line 1122333344555566778888 I need to fine-tune it and get [11, 22, 3333, 44, 5555, 66, 77, 8888] Can this be done in a beautiful way, or do I need to write it hard and use the string.substring(beginning, ending) function eight times string.substring(beginning, ending) and then put in an array ?

Edit: A string can contain more than duplicate numbers. AB CG HERD KJ 98HQ 0K 1E OOQW This is also an example!

+4
source share
7 answers

use pattern: ((\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)); } 

gives:

 11 22 3333 44 5555 66 77 8888 

edit: if its numbers, as well as spaces and letters, use the pattern (([\d\w\s])\2*)

+4
source

You can use regular expression of repeated characters :

 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]); 

Output:

 11 22 3333 44 5555 66 77 8888 

Important Note:

substrings array contains empty entries, since its length is equal to the length of the input string. If your string contains non-repeating consecutive characters, then this array may not have null entries. See substrings for a NullPointerException .

+3
source

there is no delimiter on this line to be able to use .split () if you have a delimiter between what you want to have a substring like 11-22-3333 -... etc. it will easy to use

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

Based on 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())); } } 

Output:

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

Is it pretty enough? This is just one line ...

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

Here's the test:

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

Output:

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

Please note that there is one very slight difficulty in this solution - the character after $1 in the replaceAll() call cannot be accessed in the input file. I chose the null character '\0' (i.e. hex zero) to be safe enough.

0
source

This is what I would do: If the string does not sort, "ABCABCABC", you can sort it by converting it to a Char [] array, and then using Arrays.sort (), turning it into 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; } 

Output:

  [AAA, BBB, CCC] 
0
source

Perhaps you want to use this:

 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); 

Output:

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

Source: https://habr.com/ru/post/1446634/


All Articles