Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 04-17-08   #1 (permalink)
Programmer
 
LyokoHaCk's Avatar
 
amd nvidia

Join Date: Aug 2006
Location: United States Inc.
Posts: 1,976

Rep: 73 LyokoHaCk is acknowledged by some
Unique Rep: 68
FAQs Submitted: 1
Folding Team Rank: 1802
Trader Rating: 2
Default Simple Java Trim Help

I can't find a way to trim the last character of a StringBuilder or a String for that matter. How can I do it? The is no trimEnd(...) and trim doesn't accept arguments.
TY
LyokoHaCk is offline I fold for Overclock.net   Reply With Quote
Old 04-18-08   #2 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

Once you go regular expressions you never go back!

Code:
str = str.replaceAll(" +$", "");
That replaces all spaces at the string end, if any are there. To just replace a single space at the end (if any), use the pattern:
Code:
" $"
or to trim the last character, regardless of what it is, use:
Code:
".$"
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200

Last edited by Scriptorum : 04-18-08 at 10:18 AM. Reason: Added clarification on the regex pattern used.
Scriptorum is offline Overclocked Account   Reply With Quote
Old 04-18-08   #3 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

If you have a String called 's'
Code:
s.substring(0, s.length()-1);
If you have a StringBuilder 'sb'
Code:
sb.deleteCharAt(sb.length()-1);
Quote:
Originally Posted by Scriptorum View Post
Once you go regular expressions you never go back!

Code:
str = str.replaceAll(" +$", "");
That replaces all spaces at the string end, if any are there. To just replace a single space at the end (if any), use the pattern:
Code:
" $"
or to trim the last character, regardless of what it is, use:
Code:
".$"
This won't actually shorten the string will it? Plus, regexps would almost certainly be slower
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 04-18-08 at 12:14 PM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-18-08   #4 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

Quote:
Originally Posted by rabidgnome229 View Post
This won't actually shorten the string will it? Plus, regexps would almost certainly be slower
It will shorten the string; replaceAll and replaceFirst replace the matched pattern with the supplied second parameter, and they return the modified string. I agree that regex is slower than a simple substring, but the difference is negligible unless you're doing thousands of string replacements in a tight loop. Your substring example is good if what the OP wants is to blindly remove the last character, but / +$/ is safer for trimming because it will chop more than one space and it will not chop if there are no spaces at the end to trim.
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200
Scriptorum is offline Overclocked Account   Reply With Quote
Old 04-18-08   #5 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by Scriptorum View Post
It will shorten the string; replaceAll and replaceFirst replace the matched pattern with the supplied second parameter, and they return the modified string.
I see - I've never used regexp in java

Quote:
I agree that regex is slower than a simple substring, but the difference is negligible unless you're doing thousands of string replacements in a tight loop.
True - I was simply mentioning the speed difference.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-18-08   #6 (permalink)
Programmer
 
LyokoHaCk's Avatar
 
amd nvidia

Join Date: Aug 2006
Location: United States Inc.
Posts: 1,976

Rep: 73 LyokoHaCk is acknowledged by some
Unique Rep: 68
FAQs Submitted: 1
Folding Team Rank: 1802
Trader Rating: 2
Default

Thanks guys~!
LyokoHaCk is offline I fold for Overclock.net   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 04:24 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.16037 seconds with 9 queries