PDA

View Full Version : Text Processing


darelf
11-26-2002, 02:55 PM
Question:

In TCL I would write this

set x [list A B C D]
join $x "/"


And it would return "A/B/C/D"

Is there anything similar, or a similar set of text-processing tools for Java? Good things would be: join, split, slice, etc.

Does such a thing exist already or do I have to make my own?

inkedmn
11-26-2002, 03:30 PM
java has text processing tools, but they really aren't anything like what you have there (except for split)

to slice a string, you'd use myString.substring(startingIndex [, endingIndex]). like this:

String newString = oldString.substring(0, 10);

that would return the first 10 characters of oldString.
[edit] one other thing, you don't have to have the second index in a substring() call...

String newString = oldString.substring(10);

that would return everything from the ninth character to the end of the string

split works in much the same way, except it can be passed a regex:

String newString = "hello, i, you see, have many, MANY, commas";
String[] newList = newString.split(",");

that SHOULD return an array of strings (haven't tested it).

no idea as far as join, but i've never seen anything like join in the API...

_underdog
11-26-2002, 03:44 PM
I would look at the documentation for the string class:
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/String.html
and if you are using jdk1.4 and need to use Regular Expressions you could look at the java.util.regex package:
http://java.sun.com/j2se/1.4.1/docs/api/java/util/regex/package-summary.html

sicarius
11-26-2002, 10:33 PM
Another handy text processing class is the StringTokenizer.

darelf
11-27-2002, 09:21 AM
Thanks for all the suggestions.
I've googled my heart out and searched all the docs I can find, and can't find anything even remotely like 'join'. I know python and tcl and probably perl have those, I just can't believe java doesn't.
I guess I'll have to hack one together.

Thanks again.

_underdog
11-27-2002, 11:25 AM
Seek and ye shall find. That is the cool thing about Java there is an open source API for everything.

Look at the Jakarta Commons Lang Component
http://jakarta.apache.org/commons/lang.html

the class org.apache.commons.lang.StringUtils has a join function.
http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html

darelf
11-27-2002, 03:23 PM
Excellent... thanks!!

inkedmn
11-27-2002, 04:39 PM
wow...

i just downloaded like HALF of those packages, they'll be a big help in doing the bot stuff

thanks _underdog!