1

I have to design an interface where it fetches data from machine and then plots it. I have already designed the fetch part and it fetches a string of format A&[email protected]$13400$13400$13386$13418$13427$13406$13383$13406$13412$13419$00000$00000$

First five A&B@. characters are the identifier. Please note that the fifth character is new line feed i.e. ASCII 0xA.

The function I have written -

   public static boolean checkStart(String str,String startStr){

       String Initials = str.substring(0,5);
       System.out.println("Here is start: " + Initials);       
       if (startStr.equals(Initials))
        return true;
        else
        return false;
     }

shows Here is start: A&B@. which is correct.

Question 1: Why do we need to take str.substring(0,5) i.e. when I use str.substring(0,4) it shows only - Here is start: A&B@ i.e. missing new line feed. Why is New Line feed making this difference.

Further to extract remaing string I have to use s.substring(5,s.length()) instead of s.substring(6,s.length())

i.e. s.substring(6,s.length()) produces 3409$13400$13400$13386$13418$13427$13406$13383$13406$13412$13419$00000$00000$ i.e missing the first char after the identifier A&B@.

Question 2:

My parsing function is:

public static String[] StringParser(String str,String del){
    String[] sParsed = str.split(del);
     for (int i=0; i<sParsed.length; i++) {
                     System.out.println(sParsed[i]);
              }
    return sParsed;
     }

It parses correctly for String String s = "A&[email protected]/13400/13400/13386/13418/13427/13406/13383/13406/13412/13419/00000/00000/"; and calling the function as String[] tokens = StringParser(rightChannelString,"/");

But for String such as String s = "A&[email protected]$13400$13400$13386$13418$13427$13406$13383$13406$13412$13419$00000$00000$" , the call String[] tokens = StringParser(rightChannelString,"$"); does not parse the string at all.

I am not able to figure out why this behaviour. Can any one please let me know the solution?

Thanks

3
  • What is StringParser doing? Does it use regular expressions? Commented Oct 4, 2013 at 8:59
  • @Henry Please see StringParser definition. Edited to show its functionality Commented Oct 4, 2013 at 9:02
  • @what'up The problem I asked is different, my code works for one scenario but does not work for another. Instead of negative vote .. solution would have helped me better. I am not a java expert Commented Oct 4, 2013 at 9:04

2 Answers 2

1

Regarding question 1, the java API says that the substring method takes 2 parameters:

  • beginIndex the begin index, inclusive.
  • endIndex the end index, exclusive.

So in your example

String: A&[email protected]
Index:  01234567

substring(0,4) = indexes 0 to 3 so A&B@, that's why you have to put 5 as the second parameter to recover your line delimiter.

Regarding question 2, I guess that the split method takes a regexp in parameter and $ is a special character. To match the dollar sign I guess you have to escape it with the \ character (as \ is a special char in strings so you must also escape it).

String[] tokens = StringParser(rightChannelString,"\\$");
Sign up to request clarification or add additional context in comments.

Comments

1

Q1: review the description of substring in the documentation:

Returns a new string that is a substring of this string.
The substring begins at the specified beginIndex and extends to the
character at index endIndex - 1. Thus the length of the substring
is endIndex-beginIndex. 

Q2: the split method takes a regular expression for the separator. $ is a special character for regular expressions, it matches the end of the line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.