-
August 26th, 2010, 12:16 PM
#1
String manipulation
Hi guys, I'm looking to split my string via the spaces and get all the words apart from the last one. for e.g,
get_lty("CP GF FGT LEDGER") would give me: CP GF FGT
get_lty("BLAH 45RT45 BLa 4 bklahg hi") would give me: BLAH 45RT45 BLa 4 bklahg
can someone help me? thanks
-
August 26th, 2010, 02:16 PM
#2
Re: String manipulation
Look at the String class's methods. There is one that will do it for you.
Norm
-
August 27th, 2010, 10:04 AM
#3
Re: String manipulation
String [] sections = "CP GF FGT LEDGER".split(" ");
String finalString ="";
for(int i=0 ; i<sections.length-1 ; i++){
finalString += " " + sections[i];
}
finalString = finalString.trim();
-
August 27th, 2010, 10:21 AM
#4
Re: String manipulation
That's a little long winded why not just use the lastIndexOf() method to find the location of the last space character and then the substring() method to extract the first part of the string.
-
August 27th, 2010, 10:25 AM
#5
Re: String manipulation
Good point keang, use that instead.
-
August 27th, 2010, 11:03 AM
#6
Re: String manipulation
Originally Posted by v.z.afzal@dundee.ac.uk
String [] sections = "CP GF FGT LEDGER".split(" ");
String finalString ="";
for(int i=0 ; i<sections.length-1 ; i++){
finalString += " " + sections[i];
}
finalString = finalString.trim();
While I'm sure the OPs of the couple of posts in which you have given solutions appreciate it, I myself, and a few others here, would appreciate it if you would not so freely perform the work for the OP.
We are here to help them work towards their goal, not give them free solutions, to what most of the time is a homework assignment. Giving them the answer teaches them nothing.
-
August 27th, 2010, 11:14 AM
#7
Re: String manipulation
Originally Posted by ProgramThis
While I'm sure the OPs of the couple of posts in which you have given solutions appreciate it, I myself, and a few others here, would appreciate it if you would not so freely perform the work for the OP.
Hear hear.
-
August 27th, 2010, 11:17 AM
#8
Re: String manipulation
Norm
-
August 30th, 2010, 03:42 PM
#9
Re: String manipulation
I'll add a belated forth
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25...
Andrew Rutherford
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 30th, 2010, 04:13 PM
#10
Re: String manipulation
Better late than never
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|