|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Simple Java Trim Help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#2 (permalink) | ||||||||
|
Chiefly Ignorant
|
Once you go regular expressions you never go back!
![]() Code:
str = str.replaceAll(" +$", "");
Code:
" $" Code:
".$"
__________________
Last edited by Scriptorum : 04-18-08 at 10:18 AM. Reason: Added clarification on the regex pattern used. |
||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Kernel Sanders
|
If you have a String called 's'
Code:
s.substring(0, s.length()-1); Code:
sb.deleteCharAt(sb.length()-1); Quote:
Last edited by rabidgnome229 : 04-18-08 at 12:14 PM. |
||||||||||||||
|
|
|
|
#4 (permalink) | ||||||||
|
Chiefly Ignorant
|
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.
__________________
|
||||||||
|
|
|
|
#5 (permalink) | |||||||||||||||
|
Kernel Sanders
|
Quote:
Quote:
|
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|