• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split String to Array - why doesn't this work?  RSS feed

 
Ben Hen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a PHP guy, and this is all new to me. The code below won't split my string on the pipe and I don't understand why.


Results:
Raw: 1234|5678|Ackada|Kim|kAckada@sod.com|kAckada - 3
Raw: 2222|3333|Abdullah|Marchall|mabdullah@sod.com|mabdullah - 2

It's treating stArray like a string rather than an array, returning the 2nd char instead of the 2nd element.

Why?

Thanks for helping a java dolt.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, welcome to the Ranch.

As a newcomer to JSP, you should be aware that you are writing JSP as if it were 2001. Java code in a JSP has been discredited and considered a poor practice since the introduction of JSP2 in 2002. Whatever resource you are using to learn JSP may be outdated. You should be concentrating on using the JSTL and EL and not Java scriptlets.

You might also find these articles informative:
  • The Secret Life of JSPs
  • The Front Man


  • Lastly, as your question is really on using Java string methods and not really a JSP question, I've moved this to another forum that's more appropriate to your question.
     
    Greg Charles
    Sheriff
    Posts: 3015
    12
    Firefox Browser IntelliJ IDE Java Mac Ruby
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The parameter to split is a regular expression, and | has special meaning in regular expressions. Try split("\\|") instead.
     
    Ben Hen
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Greg - you jostled my memory - yes - it's



    and that works.

    Thanks, Bear. Sorry - I'm probably all over the place with the code. I don't usually touch the stuff, so I'm using what I can find or figure out. I'm developing a fairly simple API, and the dba won't let me use PHP.
     
    Ben Hen
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, another dumb question. Why does my if statement not find the match? There is a match. They're both strings, as far as I can tell...



    This code returns nothing.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When you say it "returns nothing", do you mean that this:



    doesn't print anything?

    When you want to compare objects' states--including Strings' contents--you need to use the equals() method. Using == just compares two references to see if they point to the same object.
     
    Ben Hen
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes - the if statement never finds a match, so nothing is printed. Thanks! I'll look into the equals() method.



    THANK YOU!
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!