Java检测String中的双引号

时间:2012-10-07 05:35:05

标签: java string logic

示例代码

public static void main(String args[]){  
String test=null;   
if(some condition)  
   test = "\"abc\"";
else
   test ="def";

// if condition is true , in debug mode when i check the value of test its ""abc"" (double double quote);   
// if condition is false , then value of test is "def" (double quotes only one time);


}

寻找一个逻辑来检查字符串是否有双引号。尝试下面的东西

// test.startsWith("\"\""))  // this didn;t work

2 个答案:

答案 0 :(得分:19)

您正在检查2 "(double quotes)s,而您的字符串在开头只有一个。请尝试以下:

 test.startsWith("\"");
 test.endsWith("\"");

应该工作。

答案 1 :(得分:0)

我不完全确定你想要达到的目标,但确保在对它执行任何操作之前初始化“test”。

您只需要检查“test”是否以单引号开头,因为第一个双引号不是字符串内容的一部分。

相关问题