How to enter quotes in a Java string?

后端 未结 10 1134
不知归路
不知归路 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: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;
    }
    

提交回复
热议问题