This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

String manipulation  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Currently I am trying to do a string manipulation where in a for loop I want to append new pieces of String in front of and at the end of the existing string.
So the output should be something like this.

I have a String array = {"False","No","Yes", "True};

So all the initial values should appear twice like shown below & the last value should be shown only once.
-----------------------------------------
False No Yes True Yes No False
-----------------------------------------

Do let me know how to go about the same

Bye,
Uday
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check this code. it will give you waht do you want:::

String array[] = {"False","No","Yes", "True"};
String afterApp="";
int i;
for(i=0;i<array.length;i++){
afterApp+=array[i]+" ";
}
for(i=i-2;i>=0;i--){
afterApp+=array[i]+" ";
}
System.out.println("After Append "+afterApp);


Regards,
Venkat
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the StringBuilder class, added in Java 5. It is basically a mutable String class, and allows you to avoid iterate twice through the array.
[ January 16, 2007: Message edited by: �dne Brunborg ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!