Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple Java Trim Help
New Posts  All Forums:Forum Nav:

Simple Java Trim Help

post #1 of 6
Thread Starter 
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
post #2 of 6
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:
".$"
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #3 of 6
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
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #4 of 6
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.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #5 of 6
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.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #6 of 6
Thread Starter 
Thanks guys~!
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple Java Trim Help