I want to display an array of Strings with space between words like this:
One Two Three
I have tried this:
Display.setText( " " + swappedString);
But if i do it that way, the first word will have space before it. I don't want that space.
You can use this also :
String [] myArray = {"One","Two","Three"};
StringBuilder builder = new StringBuilder();
for (String value : myArray) {
builder.append(value).append(" ");
}
System.out.println(builder);
append(value+" "). Write it as append(value).append(" "). value+" " would need to create new StringBuilder to concatenate these values which we don't need since we already have one created.Just use trim() function of String to get rid of extra space (before or after) in the space.
See the sample code to get how it works:
String result = "";
String[] strings = new String[]{"One", "Two", "Three"};
for (String string : strings) {
result = result + string + " ";
}
result = result.trim();
System.out.println(">" + result + ">");
One outside suggesation is, use StringBuilder. It does not create object every time.Sample Code:
StringBuilder stringBuilder = new StringBuilder();
String[] strings = new String[]{"One", "Two", "Three"};
for (String string : strings) {
stringBuilder.append(string);
stringBuilder.append(" ");
}
String result = stringBuilder.toString().trim();
System.out.println(">" + result + ">");