Results 1 to 9 of 9
Thread: How to take a string litterally.
- 07-27-2010, 08:26 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 34
- Rep Power
- 0
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.
- 07-27-2010, 09:52 PM #2
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?
- 07-27-2010, 10:05 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 34
- Rep Power
- 0
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.
- 07-27-2010, 10:13 PM #4
So the @ is only for a String containing \s?What are the special characters that you need the compiler to ignore?
I guess I've never had a problem. What kind of code do you write that requires lots of escaping?I'll just have to live with lot's of escaping
- 07-28-2010, 09:47 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 07-29-2010, 01:13 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 34
- Rep Power
- 0
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.
- 07-29-2010, 01:23 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 34
- Rep Power
- 0
>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.
- 07-29-2010, 01:35 PM #8
You still haven't answered my question:
What are the special characters that you need the compiler to ignore?
- 07-29-2010, 01:39 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 34
- Rep Power
- 0
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 01:42 PM.
Jon Jacobs
This message was composed entirely from recycled electrons.
Similar Threads
-
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks