Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

splitting a string..

Thread view: 
Frances - 12 Sep 2005 19:22 GMT
does sthg like this work in java?

    String pairs = sQuery.split("&");

or can u only split strings w/reg. expressions in java?
  how would this be done w/a regular expression?  following what I
found here http://www.rgagnon.com/javadetails/java-0438.html I did

    String pairs = sQuery.split("\\&");  // but get errors..

(this is for extracting info from a query string in a jsp..)
  thank you..
Frances - 12 Sep 2005 19:37 GMT
> does sthg like this work in java?
>
[quoted text clipped - 8 lines]
> (this is for extracting info from a query string in a jsp..)
>   thank you..

I hadn't realized it returns an array, so had forgotten '[]' in var
name...  it worked w/o a regular expression,

   String pairs[] = sQuery.split("&");

although it says in docs this method takes a regular exp as argument..
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

thanks..  Frances
Frances - 12 Sep 2005 19:49 GMT
>> does sthg like this work in java?
>>
[quoted text clipped - 18 lines]
>
> thanks..  Frances

well, ACTUALLY....  it is and it isn't working...
in my Tomcat locally it works fine, but when I upload to server I get
this error..

org.apache.jasper.JasperException: Unable to compile class for
JSP/usr/local/jakarta-tomcat-3.2.2/work/localhost_8080/_0002fchat_0002findex_0002ejspindex_jsp_10.java:85:
Method split(java.lang.String) not found in class java.lang.String.
                String pairs[] = sQuery.split("&");

only diff. -- that I can see -- being Tomcat versions (5.0.27 on my
machine and -- judging by what it says here -- 3.2.2 on server..)

thank you.......
Thomas Hawtin - 12 Sep 2005 20:06 GMT
>> I hadn't realized it returns an array, so had forgotten '[]' in var
>> name...  it worked w/o a regular expression,
[quoted text clipped - 3 lines]
>> although it says in docs this method takes a regular exp as argument..
>> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

IIRC, & is only significant in replacement strings. You can use
Pattern.quote to quote literals in regexs, and Matcher.quoteReplacement
for replacement strings (from 1.5).

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html#quote(java.
lang.String
)
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#quoteReplac
ement(java.lang.String
)

> well, ACTUALLY....  it is and it isn't working...
> in my Tomcat locally it works fine, but when I upload to server I get
[quoted text clipped - 7 lines]
> only diff. -- that I can see -- being Tomcat versions (5.0.27 on my
> machine and -- judging by what it says here -- 3.2.2 on server..)

That's a really backward version of Tomcat. Presumably it has unpatched
known security flaws.

Looks as if it is running on JRE 1.3 (or earlier), which doesn't have
String.split or regexs.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 12 Sep 2005 21:14 GMT
>IIRC, & is only significant in replacement strings. You can use
>Pattern.quote to quote literals in regexs, and Matcher.quoteReplacement
>for replacement strings (from 1.5).

The rules for when you need the quoting can be a bit boggling, since
they differ for search and replacement, and for the various different
regex engines you use.  I use three different ones in a day.

I have two ways of sorting this out.

1. Quoter amanuensis that inserts \ quotes where needed to get he
literal meaning. See http://mindprod.com/applets/quoter.html

2. the happy rule that is ok to quote even when you don't need it. So
most of the time I just quote any awkward looking character.
That protects me from some future regex where new magic chars are
introduced.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 12 Sep 2005 21:11 GMT
>it worked w/o a regular expression,

& String.split() ALWAYS takes a regular expression. It is just that
some regular expressions are so simple they are also single
characters.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Oliver Wong - 12 Sep 2005 21:17 GMT
> I hadn't realized it returns an array, so had forgotten '[]' in var
> name...  it worked w/o a regular expression,
[quoted text clipped - 3 lines]
> although it says in docs this method takes a regular exp as argument..
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

   I believe "&" is a valid regular expression, as is "Hello" for example.

   - Oliver
Roedy Green - 12 Sep 2005 21:02 GMT
> String pairs = sQuery.split("\\&");  // but get errors..

See http://mindprod.com/jgloss/regex.html

// Pattern to split into words separated by spaces or commas, ignoring
null fields
// In other words, anything matching this pattern is considered a
place to break words.
private static Pattern splitter = Pattern.compile ("\\&" );
...
// Split phrase into words
String[] words = splitter.split( phrase );
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.