• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

adding number to string

I am using c++ and java and i have a return string of ten words and i would like to display a number for each word from the java side. how can i add the number to each word when the string holds ten words?
 
Assuming your words are separated by a space character:

Code:
  String str = "here is some text";
  String words[] = str.split(" ");
  StringBuilder sb = new StringBuilder();
 int i = 0;
  for (String word : words) {
  sb.append(word).append(String.valueOf(i++));
}
 
thats great but the string to holds each word line be line with a return. I wanted to print them each the way they are in the string but I wand to add a number to each word.
for example lets say the string holds

word1
word2
word3

and i wanted to print out the words with number as
1 word1
2 word2
3 word3
 
Code:
  String str = "here is some text";
  String words[] = str.split("\n");
  StringBuilder sb = new StringBuilder();
 int i = 0;
  for (String word : words) {
  sb.append(word).append(String.valueOf(i++));
}
 
So if you used the above code to split the String into separate words, the individual words will be in the String array.
Simply iterate through the array, and set the elements as required

Code:
for (int i=0; i<words.length; i++) {
  words[i] = String.valueOf(i+1);
}
 
Here is what my string holds

1 ---- word1 5 letters
2 ---- word2 5 letters

I wanted to change the font on word1 and word2.....how can I do that?
 
Back
Top Bottom