• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

split() method in String API

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys
this is my main method code


the output is


if i replace the my main method code with this


my output looks like


my Question is why split method is not functioning propery with String two[]= te.split("|");
looking for your replies
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The argument to split is a regular expression; a kind of pattern to match against. "|" is a special character in regular expressions. To use it as a plain character, you must escape it with a backslash; to make sure the regular expression parser sees the backslash, you must double it. So the proper syntax is going to be:

String two[]= te.split("\\|");
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply Ernest
but my doubt still exist

in the above case my split pattern dosent match in the string te


but in the above even though the split pattern dosent match why the characters are splited and return as an array

looking for your replies
amir
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still see this line in your code:

String two[]= te.split("|");

instead of what Ernest suggested:

String two[]= te.split("\\|");

Did you try this?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amirtharaj Chinnaraj:

but in the above even though the split pattern dosent match why the characters are splited and return as an array



The | is a special character in regular expressions that means match either the part on the left side or the part on the right side of the |. As you don't have anything on either the left or right of the |, I presume (I'm not a RE expert) it is interpreted as meaning match anything, so you get back an array with each element being a single character string.
 
Marshal
Posts: 80140
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:


The | is a special character in regular expressions that means match either the part on the left side or the part on the right side of the |. As you don't have anything on either the left or right of the |, I presume (I'm not a RE expert) it is interpreted as meaning match anything, so you get back an array with each element being a single character string.

More likely it is interpreted as "match nothing or nothing". You get a "nothing" after every letter, so it matches that and then goes to the next letter. That is why it splits the String into individual single letters.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic