• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split a string  RSS feed

 
pradipta kumar rout
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected sir,
Kindly give some idea about how to split the following string where '|' is present

I have tried follow but not giving the answer
e.g


it gives as follow


pradipta
rout
|
kumar
rout
|
rout
rout


But but I need the out put should as follow

pradipta rout
kumar rout
rout rout


 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to escape the "|" character. As this is a valid regex character- You need to escape as "\\|".
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed means "regex meta character" instead of "valid regex character", but the essence of his post is spot on.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Mohamed means "regex meta character" instead of "valid regex character", but the essence of his post is spot on.


Thanks Rob for the clarification.
 
pradipta kumar rout
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,

Why to use "\\|" ,if I want to detect only '|' it should be "\|" .
kindly tell me why this ..sir

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pradipta kumar rout wrote:Why to use "\\|" ,if I want to detect only '|' it should be "\|" .

Because \ is a special character in a Java string literal. So the regular expression needs one \ to escape the |, but then the String literal needs another \ to escape the first \.

Simple, isn't it!
 
Ratan Kumar Yadav
Greenhorn
Posts: 6
Flex Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can also use the legacy class StringTokenizer from java.util package to tokenize String.

import java.util.*;
class StringToken
{
public static void main(String args[])
{
String str="pradipta rout |kumar rout|rout rout";
StringTokenizer st=new StringTokenizer(str,"|");
while(st.hasMoreTokens())
System.out.println(st.nextToken());
}
}

This is just an another way to split your string based on delimiters.
Keep Learning
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ratan Kr wrote:We can also use the legacy class StringTokenizer from java.util package to tokenize String.


Why would you want to use legacy?
 
pradipta kumar rout
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you every one .....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!