How to enter quotes in a Java string?

后端 未结 10 1122
不知归路
不知归路 2020-11-22 05:38

I want to initialize a String in Java, but that string needs to include quotes; for example: \"ROM\". I tried doing:

String value = \" \"ROM\" \         


        
相关标签:
10条回答
  • 2020-11-22 06:00

    In reference to your comment after Ian Henry's answer, I'm not quite 100% sure I understand what you are asking.

    If it is about getting double quote marks added into a string, you can concatenate the double quotes into your string, for example:

    String theFirst = "Java Programming";
    String ROM = "\"" + theFirst + "\"";
    

    Or, if you want to do it with one String variable, it would be:

    String ROM = "Java Programming";
    ROM = "\"" + ROM + "\"";
    

    Of course, this actually replaces the original ROM, since Java Strings are immutable.

    If you are wanting to do something like turn the variable name into a String, you can't do that in Java, AFAIK.

    0 讨论(0)
  • 2020-11-22 06:01

    In Java, you can use char value with ":

    char quotes ='"';
    
    String strVar=quotes+"ROM"+quotes;
    
    0 讨论(0)
  • 2020-11-22 06:02

    Here is full java example:-

    public class QuoteInJava {     
    
    public static void main (String args[])
        {
                System.out.println ("If you need to 'quote' in Java");
                System.out.println ("you can use single \' or double \" quote");
        }
    }
    

    Here is Out PUT:-

    If you need to 'quote' in Java
    you can use single ' or double " quote
    

    0 讨论(0)
  • 2020-11-22 06:04

    Just escape the quotes:

    String value = "\"ROM\"";
    
    0 讨论(0)
  • 2020-11-22 06:08

    This tiny java method will help you produce standard CSV text of a specific column.

    public static String getStandardizedCsv(String columnText){
    
        //contains line feed ?
        boolean containsLineFeed = false;
        if(columnText.contains("\n")){
            containsLineFeed = true;
        }
    
        boolean containsCommas = false;
        if(columnText.contains(",")){
            containsCommas = true;
        }
    
        boolean containsDoubleQuotes = false;
        if(columnText.contains("\"")){
            containsDoubleQuotes = true;
        }
    
        columnText.replaceAll("\"", "\"\"");
    
        if(containsLineFeed || containsCommas || containsDoubleQuotes){
            columnText = "\"" + columnText + "\"";
        }
    
        return columnText;
    }
    
    0 讨论(0)
  • 2020-11-22 06:10

    \ = \\

    " = \"

    new line = \r\n OR \n\r OR \n (depends on OS) bun usualy \n enough.

    taabulator = \t

    0 讨论(0)
提交回复
热议问题