Java string: classes or packages with advanced functions?
I am doing string manipulations and I need more advanced functions than the original ones provided in Java. For example, I'd like to return a substring between the (n-1)th and nth occurrence of a character in a string. My question is, are there classes already written by users which perform this function, and many others for string manipulations? Or should I dig on stackoverflow for each particular function I need?
Check out the Apache Commons class StringUtils, it has plenty of interesting ways to work with Strings. http://commons.apache.org/lang/api-2.3/index.html?org/apache/commons/lang/StringUtils.html
Have you looked at the regular expression API? That's usually your best bet for doing complex things with strings: http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html Along the lines of what you're looking to do, you can traverse the string against a pattern (in your case a single character) and match everything in the string up to but not including the next instance of the character as what is called a capture group. It's been a while since I've written a regex, but if you were looking for the character A for instance, then I think you could use the regex A([^A]*) and keep matching that string. The stuff in the parenthesis is a capturing group, which I reference below. To match it, you'd use the matcher method on pattern: http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#matcher%28java.lang.CharSequence%29 On the Matcher instance, you'd make sure that matches is true, and then keep calling find() and group(1) as needed, where group(1) would get you what is in between the parentheses. You could use a counter in your looping to make sure you get the n-1 instance of the letter. Lastly, Pattern provides flags you can pass in to indicate things like case insensitivity, which you may need. If I've made some mistakes here, then someone please correct me. Like I said, I don't write regexes every day, so I'm sure I'm a little bit off.
How do pass multiple data types to HashMap
Why transactions are not rolling back in Spring while running with Junit
How do I compare this string to my array?
Java code for SSL not recognizing SAN
Create a basic controller Spring MVC in Intellij
How to set value to variable for first run in android
Java is giving me different results for the same process
How to encode BSON Int64 as JSON String using Mongo Java client
Create a file on startup in java
Android Studio Bluetooth App. Spanish charset issue
soa-model: How to specify xs:choice element to use in a generated soap message?
OpenCV [Template Matching] “java.lang.UnsupportedOperationException: Mat data type is not compatible: 5”
JAVA PhantomJS not working
How to read value in CSV file
Saving an object from data structure and then erasing it from it
Ugly nested loop algorithm needs improvement