24

I need to insert a space after every character of a string.

i.e. String name = "Joe";

should become: "J o e"

9 Answers 9

45

Shorter would be using a regex:

System.out.println("Joe".replaceAll(".(?!$)", "$0 "));
Sign up to request clarification or add additional context in comments.

4 Comments

This has the added advantage (compared to looping over the characters) that it doesn't break surrogate pairs.
Now you have two problems. stackoverflow.com/questions/1038186/…
@Wouter: what do you mean by surrogate pairs?
@StuperUser: Pairs of characters used to represent characters outside the "Basic Multilingual Plane". See class description on download.oracle.com/javase/6/docs/api/java/lang/Character.html
27

For Android & Kotlin users, if you want to add space after every X characters then use this

val stringWithSpaceAfterEvery4thChar = stringWith12Chars?.replace("....".toRegex(), "$0 ")

Here I added 4 dots in method to add space after every 4th character in my whole string. If you want space after every 2 characters then add only 2 dots in the method.

My variables:

stringWith12Chars = "123456789012"

and the output would be,

stringWithSpaceAfterEvery4thChar = "1234 5678 9012"

2 Comments

this is just perfect!
You gave proper logic.
10

Something like:

String joe = "Joe";
StringBuilder sb = new StringBuilder();

for (char c: joe.toCharArray()) {
   sb.append(c).append(" ");
}

System.out.println(sb.toString().trim());

1 Comment

A slight optimization: sb.setLength(sb.length() > 0 ? sb.length() - 1 : 0).toString(); i.e. trim the builder, not the result.
7

This will space out all letters in each word and not between words

"Joe Black".replaceAll("\\B", " ") -> "J o e B l a c k"

This will put space for each character (including original spaces)

"Joe Black".replaceAll("\\B|\\b", " ") -> " J o e  B l a c k "

Comments

3

Using kotlin

val textToBeDevided = "123456789" 
textToBeDevided
   .subSequence(1, textToBeDevided.length) 
   .chunked(3) // group every 3 chars
   .joinToString(" ") // merge them applying space

1 Comment

.subSequence call is redundant because String is already a CharSequence. Moreover, indexes in strings start from 0 so you've lost "1" in an output.
0
char[] stringArray = strOrig.toCharArray(); 
StringBuilder sb = new StringBuilder();

for(int index=0; index < stringArray.length; index++) {
   sb.append(stringArray[index]);
   sb.append(" ");
}

Comments

0

Solution without regex

name.chars().mapToObj(i -> (char) i + " ").collect(Collectors.joining()).strip()

Don't like regex because compile method slow

Comments

-1

You can convert Joe to char[] by using String's toCharArray() then traverse char[] to grab the char into another char[] and as you add the char to the second char[], you add a space character '" "'. Set a if-else within the loop to detect the last character so that you wouldn't add a space character by accident behind the last character. Use a String to valueOf() the resulting char[] to turn it into a String object.

Comments

-1

Removing the final space:

String joe = "Joe"; 
StringBuilder sb = new StringBuilder(); 
String sep = "";
for (char c: joe.toCharArray()) { 
    sb.append(sep).append(c);
    sep = " ";
} 

System.out.println(sb.toString()); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.