Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: VM re-evaluating a string

  1. #1
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default VM re-evaluating a string

    First of, sorry for the poor title, I dont know how to describe it in a better way.

    My problem is as follows. I have a string where I need to do the vm's escape evalutation twice.

    One time evalutation:
    		String s = "\b";
    		System.out.println(s.charAt(0));
    		System.out.println((int) s.charAt(0));
    output:
    
    8

    The wm reads the escape (when the string gets autoboxed I think) and translate it into backslash, with the charcode 8. All is as espected.

    However, I need to redo the evaluation, so that can get the same result but from the string "\\b".
    		String s = "\\b";
    		System.out.println(s.charAt(0));
    		System.out.println(s.charAt(1));
    		String s2 = "" + s.charAt(0) +  s.charAt(1); 	//Here is where, I need to re evaluate the escape
    											//Instead i get the "\b"
    		System.out.println(s2);
    output:
    \b

    I also tried
    		String s = "\\b";
    		char a = s.charAt(0);
    		char b = s.charAt(1);
    		char c = (char) (a + b);
    		System.out.println(c);
    		System.out.println((int) c);
    output:

    190

    Can this even be done? Hope there is an idea out there. Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: VM re-evaluating a string

    get the same result but from the string "\\b".
    Is the same result is the single letter: b

    What is it you are trying to do? If the String (at execution time) contains "\b" what do you want get from it?
    What about if the String contains "\\\\\b" what do you want to get back?

    After the compiler has done the escaping, it generates a String. The String can contain one or more \ characters.

    vm's escape evalutation
    Actually it is NOT the JVM that does the escaping. Its the compiler.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Johannes (September 20th, 2010)

  4. #3
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: VM re-evaluating a string

    Quote Originally Posted by Norm View Post
    Is the same result is the single letter: b

    What is it you are trying to do? If the String (at execution time) contains "\b" what do you want get from it?
    What about if the String contains "\\\\\b" what do you want to get back?

    After the compiler has done the escaping, it generates a String. The String can contain one or more \ characters.

    Actually it is NOT the JVM that does the escaping. Its the compiler.
    I want the compiler to do escaping one more time.

    Take string "\\b"
    Do escaping -> "\b"
    Take string "\b"
    Do escaping -> 'backslash' (escape symbol)

    The reason being Im trying to make my own escape function as in a java compiler (written in java).

    So for testing Im inputting a string that contains "\\b", to simulate a string with "\b"

    Hopes that makes sense.

    PS. Dont know why I always refere to the jvm as the complete java entity, when its clearly wrong.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: VM re-evaluating a string

    Not sure I follow. Can you give some examples in two columns.
    Column one would be what is in the String at execution time (after the compiler has done all the escaping)
    Coumn two would be the results you want to obtain by reworking the contents of column one.

  6. #5
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: VM re-evaluating a string

    	//String at exe				//Wanted Result
    	\b						(backspace)
    	\\						\
    	dasfasdf\\addaf				dasfasdf\addaf

    But since the string at execution is already escaped, I cant figure out how to do it again.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: VM re-evaluating a string

    I cant figure out how to do it again.
    You have to play compiler. When you see a \ you have to look at the next character and create the "escaped" character.
    A switch should do it:
      switch (theChar) {
       case 'b': return '\b';
       case '\':  return '\';
       case 'n':  return '\n';
    }

  8. #7
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: VM re-evaluating a string

    Yeah thats what I ended up with, a switch case followed by 2 different regex, one for "normal" escapes and one for octals, then followed by other switches. I just thought it would be more elegant to just return a char from the related part of the string. This way the end result becomes a rather deep nesting of conditionals. Thanks for you time btw

Similar Threads

  1. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  2. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 10:16 AM
  3. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 03:28 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM