tags:

views:

319

answers:

6

I want to make strings like "a b c" to "prefix_a prefix_b prefix_c"

how to do that in java?

+3  A: 

Assuming a split character of a space (" "), the String can be split using the split method, then each new String can have the prefix_ appended, then concatenated back to a String:

String[] tokens  = "a b c".split(" ");
String result = "";

for (String token : tokens) {
    result += ("prefix_" + token + " ");
}

System.out.println(result);

Output:

prefix_a prefix_b prefix_c

Using a StringBuilder would improve performance if necessary:

String[] tokens  = "a b c".split(" ");
StringBuilder result = new StringBuilder();

for (String token : tokens) {
    result.append("prefix_");
    result.append(token);
    result.append(" ");
}
result.deleteCharAt(result.length() - 1);

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

The only catch with the first sample is that there will be an extraneous space at the end of the last token.

coobird
you could .trim() that last space away, since the requirement does not indicate the presence of leading or trailing spaces.
ninesided
@ninesided: That's a good idea, thanks for the suggestion :)
coobird
A: 

hope I'm not mis-reading the question. Are you just looking for straight up concatenation?

String someString = "a";
String yourPrefix = "prefix_"; // or whatever
String result = yourPrefix + someString;

System.out.println(result);

would show you

prefix_a
JustJeff
woops. thought you had "a" "b" "c" there. So yeah, use String.split first to get the pieces.
JustJeff
A: 

This is C# but should easily translate to Java (but it's not a very smart solution).

String input = "a b c";
String output (" " + input).Replace(" ", "prefix_")

UPDATE

The first solution has no spaces in the output. This solution requires a place holder symbol (#) not occuring in the input.

String output = ("#" + input.Replace(" ", " #")).Replace("#", "prefix_");

It's probably more efficient to use a StringBuilder.

String input = "a b c";

String[] items = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

StringBuilder sb = new StringBuilder();

foreach (String item in items)
{
    sb.Append("prefix_");
    sb.Append(item);
    sb.Append(" ");
}

sb.Length--;

String output = sb.ToString();
Daniel Brückner
Cause for the down vote? Language intolerance? :D
Daniel Brückner
A: 

You can use StringTokenizer to enumerate over your string, with a "space" delimiter, and in your loop you can add your prefix onto the current element in your enumeration. Bottom line: See StringTokenizer in the javadocs.

You could also do it with regex and a word boundary ("\b"), but this seems brittle.

Another possibility is using String.split to convert your string into an array of strings, and then loop over your array of "a", "b", and "c" and prefix your array elements with the prefix of your choice.

marc esher
A: 

You can split a string using regular expressions and put it back together with a loop over the resulting array:

public class Test {
    public static void main (String args[]) {
        String s = "a b c";
        String[] s2 = s.split("\\s+");
        String s3 = "";
        if (s2.length > 0) 
            s3 = "pattern_" + s2[0];
        for (int i = 1; i < s2.length; i++) {
            s3 = s3 + " pattern_" + s2[i];
        }
        System.out.println (s3);
    }
}
paxdiablo
+4  A: 

You can use the String method: replaceAll(String regex,String replacement)

String s = "a xyz c";
s = s.replaceAll("(\\w+)", "prefix_$1");
System.out.println(s);

You may need to tweek the regexp to meet your exact requirements.

objects
thanks oscar :)
objects
Oh it won't work when string is like "multiple keywords",I need it to change into "prefix_multiple prefix_keywords"
Shore
updated answer to support words
objects