如何在 Java 字符串中输入引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3559063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:51:07  来源:igfitidea点击:

How to enter quotes in a Java string?

javastringquotesescaping

提问by jimmy

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

我想在 Java 中初始化一个字符串,但该字符串需要包含引号;例如:"ROM"。我试着做:

String value = " "ROM" ";

but that doesn't work. How can I include "s within a string?

但这不起作用。如何"在字符串中包含s?

采纳答案by Ian Henry

In Java, you can escape quotes with \:

在 Java 中,您可以使用以下命令转义引号\

String value = " \"ROM\" ";

回答by Shaggy Frog

Not sure what language you're using (you didn't specify), but you should be able to "escape" the quotation mark character with a backslash: "\"ROM\""

不确定您使用的是什么语言(您没有指定),但您应该能够使用反斜杠“转义”引号字符: "\"ROM\""

回答by Ian Henry

Just escape the quotes:

只需转义引号:

String value = "\"ROM\"";

回答by GreenMatt

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

关于您在 Ian Henry 回答后的评论,我不能 100% 确定我理解您的要求。

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 变量来做,那就是:

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

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

当然,这实际上取代了原来的 ROM,因为 Java 字符串是不可变的。

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

如果您想做诸如将变量名转换为字符串之类的操作,则不能在 Java AFAIK 中执行此操作。

回答by kumar

In Java, you can use char value with ":

在 Java 中,您可以将 char 值与 ":

char quotes ='"';

String strVar=quotes+"ROM"+quotes;

回答by Oleksandr Samsonov

\ = \\

\ = \\

" = \"

" = \"

new line = \r\nOR \n\rOR \n(depends on OS) bun usualy \nenough.

新行 = \r\nOR \n\rOR \n(取决于操作系统)通常\n足够了。

taabulator = \t

制表符 = \t

回答by Madan Sapkota

Look into this one ... call from anywhere you want.

看看这个……从你想要的任何地方打电话。

public String setdoubleQuote(String myText) {
    String quoteText = "";
    if (!myText.isEmpty()) {
        quoteText = "\"" + myText + "\"";
    }
    return quoteText;
}

apply double quotes to non empty dynamic string. Hope this is helpful.

对非空动态字符串应用双引号。希望这是有帮助的。

回答by Isharth Rastogi

suppose ROM is string variable which equals "strval" you can simply do

假设 ROM 是字符串变量,它等于“strval”,你可以简单地做

String value= " \" "+ROM+" \" ";

it will be stored as

它将被存储为

value= " "strval" ";    

回答by Vipul Gulhane

Here is full javaexample:-

这是完整的Java示例:-

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

enter image description here

在此处输入图片说明

回答by mifthi

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

这个微小的 java 方法将帮助您生成特定列的标准 CSV 文本。

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;
}