banner



How To Put Quotes In A String Java

Download Article

Download Commodity

  • Using an Escape Character
  • |
  • Using ASCII Code

You lot've discovered that the double quote symbol " volition not work inside a Java print education. You'll need to notice an alternate way to tell the compiler to impress this symbol, instead of interpreting it as instructions to close the string. The escape graphic symbol is the most straightforward way to do this. While in that location's no demand to acquire the ASCII code in this case, it's another handy option to know for symbols that do non have an escape sequence.

  1. 1

    Type the escape grapheme \. Every bit yous know, the double quote symbol " has special meaning in Coffee (displaying text). Whenever you lot want to ignore one of these meanings, use the escape character \ (backlash). This graphic symbol tells the compiler that the next character is part of an alternate instruction.

    • Make sure you are hitting the backslash cardinal, not the forward slash. The backslash fundamental is next to the } cardinal on near English language keyboards.
  2. two

    Type \" to display the double quote. These two characters together are called an escape sequence. Each escape sequence has a special meaning. In this example, \" just means "insert a double quote symbol hither,", without interpreting it as the start or end of text.

    • You will need to use this sequence for each individual double quote yous want to display.

    Advertisement

  3. 3

    Go along your code every bit usual. The escape sequence does not affect the rest of your code. In that location is no demand to type anything else to return to normal programming.

  4. four

    Remember to insert ordinary Java quotes as needed. Ane mutual fault is to get out out the plain former " mark in your program. Remember that \" is just for display, and does not remove the need to encase your brandish text in quotation marks. Here's an instance:

    • one. The string for displaying "Hello" is \"Hello\"
    • 2. To instruct the compiler to print this text, we wrap information technology in quotes: "\"Hullo\"".
    • 3. Here's what this looks like in a complete line of code:
                                                          System                          .                          out                          .                          println                          (                          "\"Howdy\""                          );                        
  5. Advertisement

  1. 1

    Use char(34) to stand for double quotes. Java can easily correspond ASCII symbols using the char blazon. 34 is the ASCII code for the " symbol, so write char(34) to brandish " without using its special pregnant.

    • You lot can look upwards a symbol'southward ASCII code past searching for an ASCII code table online.
  2. 2

    Place this code exterior of the impress string. If you make the mistake of putting this code inside the string, your program will print it exactly every bit it appears in your plan: char(34). Here'southward the proper method of displaying "Hello" (with the quotation marks) using this method:

                                                      Organisation                        .                        out                        .                        println                        ((                        char                        )                        34                        +                        "Hello"                        +(                        char                        )                        34                        );                      
  3. Advertisement

Add New Question

  • Question

    I put System.out.println(char)34 + (char)40 + "ACE" + (char)41 + (char)34); but output is 74ACE"). How do I fix it?

    Community Answer

    You oasis't put an apostrophe (') before the Char value. It is compulsory to keep your character constant in apostrophes, e.thou. char ch = '34';.

  • Question

    How do I print the % symbol later on whatsoever decimal value in Java?

    Pingu

    Y'all demand to utilize the printf() role. Since the % is used every bit a format sign, you demand to escape it to literally write %, which is done past writing %%. For case, if you take your decimal value stored in a variable decValue, you lot can write: printf("%f %%\n", decValue); .

  • Question

    How practice I print \ in Java?

    Community Answer

    The backslash character is used in Java and many other programming languages as an escape character. If you'd like to use the backslash character as normal, escape it properly ("\\").

Enquire a Question

200 characters left

Include your email address to get a bulletin when this question is answered.

Submit

Advertisement

  • Hither'south a list of other Escape Sequences in Coffee:

    • \t - Insert a tab in the text at this point.
    • \b - Insert a backspace in the text at this point.
    • \due north - Insert a newline in the text at this point.
    • \r - Insert a carriage render in the text at this signal.
    • \f - Insert a formfeed in the text at this point.
    • \' - Insert a single quote grapheme in the text at this point.
    • \" - Insert a double quote grapheme in the text at this betoken.
    • \\ - Insert a backslash character in the text at this point.

Thanks for submitting a tip for review!

Advertizing

Virtually This Commodity

Thanks to all authors for creating a folio that has been read 182,800 times.

Is this article up to appointment?

Source: https://www.wikihow.com/Print-Double-Quotes-in-Java

0 Response to "How To Put Quotes In A String Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel