startsWidth not working in case of pipe (|) seprator

Hi,
I have to remove the first character which is | from the text file,i tried with startswith it is not working,
If i use  comma as instead of pipe whole code works fine,only in case of pipe it is not working.
Please let me know how to solve it.

fileObj = new File(filePath);
            inputFS = new FileInputStream(fileObj);
            br = new BufferedReader(new InputStreamReader(inputFS));
            // read the first line
            String firstLine = br.readLine();
            // check if first character is pipe always return false even if the file has pipe as first character
            boolean isFirstCharPipe = firstLine.startsWith(Pattern.quote("|"));

Open in new window


Sample Text File
|Name|Age|Gender|Address|
Tech NoviceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
If i use  comma as instead of pipe whole code works fine

Doesn't work for me.
I think you are complicating things here. Please try the following code.
boolean isFirstCharPipe = firstLine.startsWith("|"); 
System.out.println(" isFirstCharPipe " + isFirstCharPipe); // prints true
String pattern = Pattern.quote("|");
System.out.println(" pattern is : " + pattern); // prints \Q|\E   where \Q means "start of literal text" and \E means "end of literal text"
System.out.println(" Pipe matches " + "|".matches(Pattern.quote("|")));  // prints true 

Open in new window

0
Tech NoviceAuthor Commented:
Thanks it is working ... i have a question what about the below code

firstLine.split("|")  //this will not work
firstLine.split(Pattern.quote("|")) this will work

Open in new window


in case of startwith
firstLine.startsWith("|"); //this works
firstLine.split(Pattern.quote("|")) //this dosent'w work

Open in new window


i m bit confused what can be the reason why | is working different ways?
0
rrzCommented:
import java.util.regex.*;
public class Test {
    public static void main(String arg[]){
		String firstLine = "|Name|Age|Gender|Address|";
		String[] wordArray1 = firstLine.split("\\|"); // the method split expects a regular expression
		System.out.println(" length of wordArray1 is " + wordArray1.length);
		//Calling the Pattern.quote() method wraps the string in \Q...\E, which turns the text is into a regex literal
        String[] wordArray2 = firstLine.split(Pattern.quote("|")); 
        System.out.println(" length of wordArray2 is " + wordArray2.length);
		boolean isFirstCharPipe = firstLine.startsWith("|"); // the method startsWith expects a String
		System.out.println(" isFirstCharPipe " + isFirstCharPipe);
    }	
}

Open in new window

Does this code help you?
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
firstLine.split(Pattern.quote("|")) //this dosent'w work

Open in new window

What makes you say that? If firstLine were to start with a pipe character the first element of the resulting array would be empty. That's how it should work.
The pipe character is a reserved character in regex meaning logical OR, which is why it needs quote() or escaping
0
rrzCommented:
@CEHJ, thanks for adding your explanation.  Yes, the first element of the array above here is an empty String.  Demonstration;
String firstLine = "|Name|Age|Gender|Address|";
String[] splitOnEmpties = firstLine.split("|");
System.out.println(" length of splitOnEmpties is " + splitOnEmpties.length); 
//prints 25

Open in new window

0
CEHJCommented:
// check if first character is pipe always return false even if the file has pipe as first character
            boolean isFirstCharPipe = firstLine.startsWith(Pattern.quote("|"));

Open in new window

Oops. Your comment confused me. I was about to ask why you'd want to do that. Then i realised you meant "always returns false". The explanation is as follows:

a. Pattern.quote("|") returns literal String "\Q|\E"
b. String.startsWith does not use regex, so it will return false unless the string happens to start with the above literal

Incidentally, there's nothing wrong with using a pipe character for a separated text file, but it shouldn't start with one, nor end with one (unless you really mean to omit fields and that cannot be the case with the header if it exists)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
>>I'm bit confused what can be the reason why | is working different ways?<<
It's not that the pipe is working in different ways, it's the method calling it that treats it differently.
In the case of a string employing the split method, the parameter is a regular expression. So firstLine.split("|") does not work but firstLine.split("\\|") will work fine (Note the two slashes since the pipe needs to be escaped by the slash since it is a reserved character and the slash itself is a reserved character so it needs an additional escape). In the case of a string employing the startsWith method, the parameter is a Char sequence, so firstLine.startsWith("|") works fine, but startsWith(Pattern.quote("|") does not because the Pattern.quote() parameter is a regular expression.
0
Tech NoviceAuthor Commented:
Thanks for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.