emi
emi

Reputation: 2950

Putting a space in a string

Assuming I have a String like "MikeJackson" I am trying to figure out a way to put a space in between so it becomes "Mike Jackson". And then applying the same method to another string say "JohnBull" would give me back "John Bull". This is the code I came up with:

public class Test{

    public Test(){

    }
public void sep(String s){
    s = s + " ";
   char[] charArray = s.toCharArray();
   int l = s.length();
for (int i = 0; i < l; i++){
    char p = ' ';

    if(Character.isUpperCase(s.charAt(0))){
        continue;   
    }
    else if (Character.isUpperCase(s.charAt(i))){
        int k = s.indexOf(s.charAt(i));
        charArray[l] = charArray[--l];
        charArray[k-1] = p;
    }
    //System.out.println(s.charAt(i));
}
}
    public static void main (String args[]){

    Test one = new Test();

    one.sep("MikeJackson");
    }  
}

My idea was to add a space to the String so that "MikeJackson" becomes "Mike Jackson " and then shift the characters on place to the right (check for where I find an uppercase) ignoring the first uppercase. Then put a character ' ' in place of the character 'J' but shift 'J' to the right. That's what I was trying to achieve with my method but it looks I need some guidelines. If anyone could help. Thanks.

Upvotes: 2

Views: 32376

Answers (7)

Silviu Burcea
Silviu Burcea

Reputation: 5348

Try this:

"MikeJackson".replaceAll("(?!^)([A-Z])", " $1");

For every upper char I am adding a space before.

Also, it works with multiple uppercase words. I am getting Word1 Word2 Word3 for Word1Word2Word3.

Upvotes: 18

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

public static void sep(String s) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {

        result.append(s.charAt(i));
        if (i != s.length() -1 && Character.isUpperCase(s.charAt(i + 1))) {
            result.append(" ");
        }
    }
    System.out.println(result);
}

Simply add a space if the next character is uppercase.

Upvotes: 5

ferrerverck
ferrerverck

Reputation: 618

public static String addSpaces(String str) {
    StringBuilder sb = new StringBuilder();
    if (str.length() == 0) return "";
    sb.append(str.charAt(0));
    for (int i = 1; i < str.length(); i++) {
        if (Character.isUpperCase(str.charAt(i))) sb.append(" ");
        sb.append(str.charAt(i));
    }
    return sb.toString();
}

Upvotes: 0

devnull
devnull

Reputation: 123458

Using String.replaceAll:

String foo = "SomeLongName";
System.out.println(foo.replaceAll("([a-z]+)([A-Z])", "$1 $2"));

Results in Some Long Name.

Upvotes: 1

npinti
npinti

Reputation: 52185

The easiest way to go round this, in this case would be to use regular expressions

    String str = "MikeJackson";
    System.out.println(str.replaceAll("(\\w+?)([A-Z])(\\w+?)", "$1 $2$3"));

Yields: Mike Jackson

Upvotes: 0

X-Pippes
X-Pippes

Reputation: 1170

Similar question here: Insert Space After Capital letter

try it and if you have any questions let us know!

the code from reference is here:

  String s = "HelloWorldNishant";
    StringBuilder out = new StringBuilder(s);
    Pattern p = Pattern.compile("[A-Z]");
    Matcher m = p.matcher(s);
    int extraFeed = 0;
    while(m.find()){
        if(m.start()!=0){
            out = out.insert(m.start()+extraFeed, " ");
            extraFeed++;
        }
    }
    System.out.println(out);

Upvotes: 1

RamonBoza
RamonBoza

Reputation: 9038

String is final and immutable, you cannot modify it, you will always use it to create a new one and assign to any variable.

Being that said, i would recommend to look for the first non-zero index uppercase, get the substring where it is located, store the two substrings and add and space between.

Upvotes: 0

Related Questions