1

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?

Kate Gregory
  • 18,565
  • 8
  • 53
  • 85
seinecle
  • 8,419
  • 13
  • 55
  • 107

2 Answers2

3

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

Cory Kendall
  • 6,656
  • 5
  • 31
  • 60
  • That's exactly what I was looking for! Thanks a lot! I'll just have to figure out how to install this in my Netbeans environment (I'm *very* new to java!) – seinecle Aug 26 '11 at 05:56
2

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.

theotherian
  • 189
  • 1
  • 9
  • Thanks I had thought of regex too, but for the sake of speed at runtime I'd rather not use regex matching for simple, general string manipulations. – seinecle Aug 26 '11 at 05:55