Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.split()

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Consider the String str = "WHAT is YOUR name;

now i am unable to split("your");

I need a way to use split without case sensitivity. Anybody can help.


Thanks alexander
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a hint .... split method accepts a regex.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Javadoc of java.util.regex.Pattern. It mentions flags for case insensitive.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help all.

I used the following. String[] spl=str.split("\\p{Lower}your");

Is this correct. I am not getting the output.

Thanks once again.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I have used this, String[] spl=str.split("[your,YOUR]");

This working partially for me. It can split even if i give YOUR as your or Your or YouR. The problem is that it splits even is i give YOU (in any case). This is the drawback.

Help me out a little please.

Thanks and Regards
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adeeb alexander wrote:Hi all.
I have used this, String[] spl=str.split("[your,YOUR]");

This working partially for me. It can split even if i give YOUR as your or Your or YouR. The problem is that it splits even is i give YOU (in any case). This is the drawback.

Help me out a little please.

Thanks and Regards


You really want to read through the regex tutorial because what you've got up there isn't even close to what you want. Please check out these two tutorials, the first for the basics, the latter for the more advanced:

Sun's basic regex tutorial
Excellent more advanced regex tutorial

in that last tutorial, you'll want to especially have a look at this page (hint, hint):
page with your answer
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot pete.

That really helped me.
I used the following. String[] spl=str.split("(?i)your");

I thought of learning regex at once. I think this really takes some time and experience. Anyways thanks again.

Thanks
alexander
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adeeb alexander wrote:Thanks a lot pete.

That really helped me.
I used the following. String[] spl=str.split("(?i)your");


You're quite welcome. The regex I used was similar to yours above but also got rid of white space if it exists:

(corrections from any regex guru is most welcome!).

I thought of learning regex at once. I think this really takes some time and experience.

It seems that nowadays, if you do any Java programming, regex is becoming indispensable, and if you're going to use it, you'd might as well learn it. I'm at the stage now where I can write regex code only when I have a nearby browser window open to one or the other tutorial. I am hopeful that in time this will improve.

Cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic