【问题标题】:Java detect double double quotes in StringJava检测字符串中的双双引号
【发布时间】:2012-09-27 19:09:42
【问题描述】:

示例代码

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

【问题讨论】:

  • if(test.startsWith("\"\"")){ System.out.println("has double double qoutes"); } 为我工作
  • if (test.charAt(0) == 34 && test.charAt(test.length()-1) == 34) { System.out.println("有双引号"); ascii 表中的 34 代表 doublequote ,因此它工作正常。
  • @vandey:谢谢,是的,这行得通

标签: java string logic


【解决方案1】:

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

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

应该可以。

【讨论】:

  • 谢谢,如何删除这些?
  • 你可以使用replaceAll方法。
【解决方案2】:

我不完全确定您想要实现什么,但请确保在对其执行任何操作之前初始化“测试”。

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

【讨论】:

    猜你喜欢
    • 2014-09-20
    • 2014-09-18
    • 2014-09-26
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多