Results 1 to 9 of 9
  1. #1
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default How to take a string litterally.

    I'm brand new to Java. I'm used to programming in C#, where, if a string literal has certain special characters, I can precede it with @ and the compiler takes the string literally without trying to interpret the special characters. I can also escape the characters with \, but the @ allows me to avoid that.

    Is there something similar in Java? (obviously not @)

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What is the difference between using the @ character and using the \ character? Why is one different than the other? What are the special characters that you need the compiler to ignore?
    Can you give an example of each?

  3. #3
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    Not much difference if you only have to escape 1 character, but a big difference if the string has 20 characters to escape and you only have to put a single @ in front of it instead. Your question suggests to me that Java does NOT have anything like that. Oh, well.

    As an example, I hate to see long paths full of double backslashes. They're harder to read and downright ugly. Many times I have to copy and paste one of those literals into a browser to test the correctness of the path. But first I have to edit out all the double backslashes. Prone to human error, too.

    Not a big deal, but it was a nice convenience in C#. And something I didn't even have to worry about in Delphi. Now that I'm starting to program in Java, I'll just have to live with lot's of escaping. It's not the end of the world.

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What are the special characters that you need the compiler to ignore?
    So the @ is only for a String containing \s?

    I'll just have to live with lot's of escaping
    I guess I've never had a problem. What kind of code do you write that requires lots of escaping?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JonJacobs View Post
    As an example, I hate to see long paths full of double backslashes. They're harder to read and downright ugly. Many times I have to copy and paste one of those literals into a browser to test the correctness of the path. But first I have to edit out all the double backslashes. Prone to human error, too.
    Java understands path names which have forward slashes. It can translate them to whatever the local system recognises, so no need for escaping. In addition, hardcoding paths is not a great idea...they should generally be in some form of properties file, in which case escaping is not a problem at all.

  6. #6
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default Re: How to take a string litterally.

    >So the @ is only for a String containing \s?
    No, that was just an easy example. @ makes the C# compiler take the string literally no matter what might be in it.

    What other escapable characters might be in C# don't matter. It's evident from the rest of this message chain that Java doesn't do anything like that, which is fine. Java probably has many other helps not available in C#. Pot-ay-to Pot-ah-to. :)

    Thanks,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  7. #7
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    >Java understands path names which have forward slashes.
    That's good to know.

    >hardcoding paths is not a great idea...
    Agreed. I don't put the hadcoding in the final version, but it comes in handy for quick tests during early stages of prototyping/designing/developing.

    Anyway, I believe I have the answer to my original question (ie: No). Which is fine. Just wanted to know.

    Thank you,
    Jon Jacobs
    This message was composed entirely from recycled electrons.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You still haven't answered my question:
    What are the special characters that you need the compiler to ignore?

  9. #9
    JonJacobs is offline Member
    Join Date
    Jul 2010
    Posts
    34
    Rep Power
    0

    Default

    Norm, I don't have any particular characters in mind. As I get started in this new (to me) language, a question came to mind, and it has been answered (No), I believe.

    After all, I don't know yet what characters (beside \) even NEED escaping in Java.

    Thanks,
    Last edited by JonJacobs; 07-29-2010 at 02:42 PM.
    Jon Jacobs
    This message was composed entirely from recycled electrons.

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 03:18 AM
  2. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 02:05 PM
  3. Replies: 5
    Last Post: 01-11-2009, 11:12 AM
  4. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •