473,698 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't do str.split("|") ?

15 New Member
Hi,

I'm trying to read a file with data seperated by "|" character.
Ex:
3578.27|2001|Ro ad Bikes|Metchosin |Canada
3399.99|2001|Mo untain Bikes|Pantin|Fr ance
3399.99|2001|Mo untain Bikes|Lebanon|U nited States

After reading the first line, I'm trying to split the string into an array by doing
Expand|Select|Wrap|Line Numbers
  1. String[] x = str.split("|");
  2.  
However java see "|" as an alternation rather than a regex delimiter for my string.

And I cannot change the data file because it has over 3000 lines.

Regards,
Gordon
Feb 18 '08 #1
7 5373
JosAH
11,448 Recognized Expert MVP
The '|' character is special to the regular expression compiler so you should escape
it with a '\' (backslash), but then again the backslash is also special to the Java
compiler (javac) so you should escape it twice; use "\\|"

kind regards,

Jos
Feb 18 '08 #2
hirak1984
316 Contributor
If the trailing fields are optional, you need to write:
Expand|Select|Wrap|Line Numbers
  1. line.split("\\|",-1);
demo:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class Splitsville {
  4.     public static void main(String[] args) {
  5.         String[] data = {
  6.             "a|b|c|d|e",
  7.             "||||",
  8.             "||c||"
  9.         };
  10.         for(String line : data) {
  11.             String[] tokens = line.split("\\|");
  12.             System.out.println("split: " + Arrays.toString(tokens));
  13.             String[] tokens2 = line.split("\\|",-1);
  14.             System.out.println("split  with -1: " + Arrays.toString(tokens2));
  15.         }
  16.     }
  17. }
Feb 18 '08 #3
r035198x
13,262 MVP
Hi,

I'm trying to read a file with data seperated by "|" character.
Ex:
3578.27|2001|Ro ad Bikes|Metchosin |Canada
3399.99|2001|Mo untain Bikes|Pantin|Fr ance
3399.99|2001|Mo untain Bikes|Lebanon|U nited States

After reading the first line, I'm trying to split the string into an array by doing
Expand|Select|Wrap|Line Numbers
  1. String[] x = str.split("|");
  2.  
However java see "|" as an alternation rather than a regex delimiter for my string.

And I cannot change the data file because it has over 3000 lines.

Regards,
Gordon
Escape the special character using \\
i.e split("\\|");
Feb 18 '08 #4
r035198x
13,262 MVP
first of all, this is not a solution. It is a suggestion rather.

Expand|Select|Wrap|Line Numbers
  1. String str = "3578.27|2001|Road Bikes|Metchosin|Canada";
  2.         String str1 = str.replace("|", ",");
  3.         String[] x = str1.split("'");
  4.         int i=0;
  5.         while (i<x.length){
  6.         System.out.println(x[i++]);
  7.         }
  8.  
What if there are words with ' in them?
Feb 18 '08 #5
hirak1984
316 Contributor
actually this works:
Expand|Select|Wrap|Line Numbers
  1. Str.split("\\|");
  2.  
Feb 18 '08 #6
r035198x
13,262 MVP
actually this works:
Expand|Select|Wrap|Line Numbers
  1. Str.split("\\|");
  2.  
Yes. That's the third time that that is being said. The explanation is in the first reply.
Feb 18 '08 #7
spoken
15 New Member
Thank you all for your replies.

Yes,
str.split("\\|" );
works perfectly.

Thanks again.. :D
Feb 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
2957
by: Joerg Schuster | last post by:
Hello, The program given below returns the lines: a ab Is there a way to use python regular expressions such that the program would return the following lines?
11
2501
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of strings. I wonder if someone else had ever stumbled on it before, and if it has a good reason to work like it is. Both implementations take two parameters: the separator character and the max number of splits (maxsplit). However, string.split() accept
35
2939
by: les_ander | last post by:
Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are name profession id #
2
1764
by: VM | last post by:
How can I separate a string that looks like this: "John|Conner|34323|T3-1" so I can place John in one variable, Conner in another variable, etc...? I know you can do it by finding the special char ('|' in this case) and calculating the size of the string, but there should be a faster way. Thanks,
6
6161
by: NuBBeR | last post by:
I need to know how to grab a substring between "{" and "}". The full text has many instances of these brackets and i need to find the string "{\\colortbl\\red0\\blue0\\green0;\\red255\\blue0\\green0\\;}" PLEASE help. very urgent. Thanks in advance!
4
4943
by: michael.bollhoefer | last post by:
I am trying to split a string into two different variables. The string is pulled from an XML file from a WEBDAV program. The string will be in this form "John Doe" <john.doe@webserver.com> the qoutes and <> are actually part of whole strinng. I need to be able to split the name into one variable and the e-mail address into another. I have been playing around with the .split but have not had much luck. Any suggestions?
3
1520
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print group_info.mess_list return linecount = 0
2
2592
by: Alexander Eisenhuth | last post by:
.... as you can find in os.py at line 1 ? Regards Alexander
2
2368
by: elgin | last post by:
I have a split Access 2003 database. I have signed the database with a Code Signing Certificate from Small Business Server. This works fine and users can have Access macro security on high or medium and do not get prompted at startup. The problem comes because there are two of us modifying the code. Whenever either one of us changes the code, one of us must resign both the front and back end of the database. If we forget to sign both, we...
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.