Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to split a string

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "\\|".
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir,

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

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 6
Tomcat Server Flex Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every one .....
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic