-1

Suppose I have a string: "VJKUKUTGCNNAUVWRKF" How can I use loops to make it into this instead "VJKUK UTGCN NAUVW RKF" where there is a space every five characters (notice the last part only has three characters).

Sri
  • 437
  • 1
  • 4
  • 13
  • 1
    Does this answer your question? [Splitting a string at every n-th character](https://stackoverflow.com/questions/2297347/splitting-a-string-at-every-n-th-character) – Max Peng Oct 28 '20 at 02:33
  • This is not a duplicate. OP asked specifically about using loops. – Aharon K Nov 02 '20 at 01:59

6 Answers6

4

Perhaps

String str = "VJKUKUTGCNNAUVWRKF";
StringBuilder sb = new StringBuilder();

for (int j = 1; j <= str.length(); j++) {
        
    sb.append(str.charAt(j-1));
        
    if (j % 5 == 0) {
        sb.append(" ");
    }
}

String str2 = sb.toString();

I don't think it's a perfectly optimized solution, but it is one way.

Aharon K
  • 312
  • 1
  • 10
2

If you are using java8 or higher :

String original  = "VJKUKUTGCNNAUVWRKF";

String modifided = Pattern.compile("(?<=\\G.{5})")
                          .splitAsStream(original)
                          .collect(Collectors.joining(" "));
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

Use String.substring() and concatenate.

String s = "JJGDVKJGFGKKGRFJJF";
String result="";

for (int i=0;i<s.length;i+=5) {
    if(!result.equals("") result+=" ";
    result+=s.substring(i, Math.max(i+5, s.length));
}

I'm writing from my mobile. So it's difficult to make a good code. There may be a few mistakes to correct.

Thomas
  • 401
  • 1
  • 6
  • 11
0

If you want to use regex please refer this

Splitting a string at every n-th character

Code:

    String s = "VJKUKUTGCNNAUVWRKF";
    System.out.println(java.util.Arrays.toString(s.split("(?<=\\G.....)")));
Sri
  • 437
  • 1
  • 4
  • 13
0

You can use some variables to keep track of the index positions and use substring(startIndex, endIndex) to update the new string. We use StringBuilder to create the new String.

String first = "VJKUKUTGCNNAUVWRKF";
StringBuilder splittedString = new StringBuilder();
int length = first.length();
int index = 0;
    
while(length > 0){
    int endIndex = index + 5;
    if(length <= 5){
        endIndex = index + length;
    }
        
    splittedString.append(first.substring(index, endIndex));
    splittedString.append(" ");
    length -= 5;
    index += 5;
}
    
System.out.println(splittedString.toString().trim());
DCruz22
  • 806
  • 1
  • 9
  • 18
0

@Aharon already give you a good solution, but it could produce redundant " " when the String length is a multiple of 5. This could fix the case and be a little more efficient and organized:

public static String split(String s, int size, String sep) {

    if (s.length() <= size) {
        return s;
    }

    final int expectedLength = s.length() + s.length() / size;

    final StringBuilder sb = new StringBuilder(expectedLength);

    sb.append(s, 0, size);

    int counter = 2 * size;

    while (counter <= s.length()) {
        sb.append(sep).append(s, counter - size, counter);
        counter += size;
    }

    if (counter - size < s.length()) {
        sb.append(sep).append(s, counter - size, s.length());
    }

    return sb.toString();
}

So you can just call:

String s = "VJKUKKASDD";

String out = split(s, 5, " ");
Nia Nia
  • 11
  • 1