Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11806 > unrolled thread

Escape character for single quotes. Is it really required in String?

Started byMausam <mausamhere@gmail.com>
First post2012-02-07 03:14 -0800
Last post2012-02-07 11:11 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Escape character for single quotes. Is it really required in String? Mausam <mausamhere@gmail.com> - 2012-02-07 03:14 -0800
    Re: Escape character for single quotes. Is it really required in String? Mausam <mausamhere@gmail.com> - 2012-02-07 03:18 -0800
    Re: Escape character for single quotes. Is it really required in String? Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-02-07 05:30 -0600
      Re: Escape character for single quotes. Is it really required in String? Mausam <mausamhere@gmail.com> - 2012-02-07 04:29 -0800
        Re: Escape character for single quotes. Is it really required in String? Knute Johnson <nospam@knutejohnson.com> - 2012-02-07 10:28 -0800
    Re: Escape character for single quotes. Is it really required in String? Lew <lewbloch@gmail.com> - 2012-02-07 11:11 -0800

#11806 — Escape character for single quotes. Is it really required in String?

FromMausam <mausamhere@gmail.com>
Date2012-02-07 03:14 -0800
SubjectEscape character for single quotes. Is it really required in String?
Message-ID<93e3af31-e4d8-43df-9c03-9645b6885d1e@l16g2000vbl.googlegroups.com>
Hello,

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089
defines escape character for single character as well.

But single quote works fine in String without any escape character.

public class Class1 {

    private static String str = "I have a single quote. That's it";
    private static String str1 = "I have a single quote. That\'s it";
    public static void main(String[] args) {
        System.out.println(str);
        System.out.println(str1);
    }
}

How the two string is different? And when should I use escape
character for single quote?

[toc] | [next] | [standalone]


#11807

FromMausam <mausamhere@gmail.com>
Date2012-02-07 03:18 -0800
Message-ID<30736796.3862.1328613513301.JavaMail.geo-discussion-forums@vbes1>
In reply to#11806
Correction :http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089
defines escape character for single quote as well.

[toc] | [prev] | [next] | [standalone]


#11808

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-02-07 05:30 -0600
Message-ID<BoednbdHwOXVlqzSnZ2dnUVZ8kudnZ2d@giganews.com>
In reply to#11806
Mausam <mausamhere@gmail.com> wrote:

> How the two string is different? And when should I use escape
> character for single quote?

In character literals.

  char apostrophe = '\'';

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#11810

FromMausam <mausamhere@gmail.com>
Date2012-02-07 04:29 -0800
Message-ID<2362811.69.1328617748528.JavaMail.geo-discussion-forums@pbcjq2>
In reply to#11808
On Tuesday, 7 February 2012 17:00:16 UTC+5:30, Leif Roar Moldskred  wrote:
> Mausam <mausamhere@gmail.com> wrote:
> 
> > How the two string is different? And when should I use escape
> > character for single quote?
> 
> In character literals.
> 
>   char apostrophe = '\'';

Thanks. For char it makes sense. But then my understanding that this escape character does not make any difference when used in String is correct.


> 
> -- 
> Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#11822

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-02-07 10:28 -0800
Message-ID<jgrqgk$sf1$2@dont-email.me>
In reply to#11810
On 2/7/2012 4:29 AM, Mausam wrote:
> On Tuesday, 7 February 2012 17:00:16 UTC+5:30, Leif Roar Moldskred  wrote:
>> Mausam<mausamhere@gmail.com>  wrote:
>>
>>> How the two string is different? And when should I use escape
>>> character for single quote?
>>
>> In character literals.
>>
>>    char apostrophe = '\'';
>
> Thanks. For char it makes sense. But then my understanding that this escape character does not make any difference when used in String is correct.
>
>
>>
>> --
>> Leif Roar Moldskred
>

and the Strings are equal;

public class Class1 {

     private static String str = "I have a single quote. That's it";
     private static String str1 = "I have a single quote. That\'s it";
     public static void main(String[] args) {
         System.out.println(str);
         System.out.println(str1);
         System.out.println(str.equals(str1));
     }
}


C:\Documents and Settings\Knute Johnson>java Class1
I have a single quote. That's it
I have a single quote. That's it
true

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#11826

FromLew <lewbloch@gmail.com>
Date2012-02-07 11:11 -0800
Message-ID<5188996.160.1328641870734.JavaMail.geo-discussion-forums@pbctz9>
In reply to#11806
On Tuesday, February 7, 2012 3:14:32 AM UTC-8, Mausam wrote:
> http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089

That's the wrong document! Why are you citing ancient information? Get current.
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

> defines escape character for single character as well.
> 
> But single quote works fine in String without any escape character.
> 
> public class Class1 {
> 
>     private static String str = "I have a single quote. That's it";
>     private static String str1 = "I have a single quote. That\'s it";
>     public static void main(String[] args) {
>         System.out.println(str);
>         System.out.println(str1);
>     }
> }
> 
> How the two string is different?

Why do you claim that they're different?

> And when should I use escape character for single quote?

When you need to in order to make the expression mean what you intend.

-- 
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web