![]() |
[JAVA] simple string conversion
i need a method that will take a string like
blah_blah_blah_(stuff).stuff and turn it into "blah blah blah" with the quotes and everything. I can't use anything based on the positions of the unwanted characters because they're different for each line (except for the extension which isn't always 3 characters anyway). So basically, I need to seek out underscores and change them to spaces, and cut the string short at the first parentheses. I can just concatonate (sp?) the quotes in there later. Thanks for any help in advance. |
My advice would be to check out the Regex api. Don't know it off the top of my head, but you can easily do that sort of replace with Regular Expressions.
its a bit late to give a more indepth answer, but I will see what i can come up with tomorrow, when I may be a little more functional. |
Doesn't java have a replace() funtion? Or if you need to dump the rest after the last underscore there should be like an instr() function you could use in conjunction with a simple loop to solve your problem.
|
i got it to work with the replace() function and indexOf() funtion. Thanks
|
Check the API docs for the String class.
You should be able to solve the core of your problem using indexOf(), substring(), and replace(). |
I think this way looks cleaner...
import java.util.regex.*; Matcher matches = null; if ((matches = Pattern.compile("^(\\d{1,2})[\\/.-](\\d{1,2})").matcher(input)).matches()) { System.out.println("First Value: "+Integer.parseInt(matches.group(1)) ); System.out.println("Second Value: "+Integer.parseInt(matches.group(2))-1 ); } |
All times are GMT -8. The time now is 09:38 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project