首先,让我们考虑一个带引号的字符串
String originalStr = "\"Demo Text\"";
现在,请看以下逻辑作为开始引用。
if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); }
现在,请看以下逻辑作为结束语。
if (originalStr.endsWith("\"")) { originalStr = originalStr.substring(0, originalStr.length() - 1); }
public class Demo { public static void main(String[] args) { String originalStr = "\"Demo Text\""; System.out.println("String with double quotes= "+originalStr); if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); } if (originalStr.endsWith("\"")) { originalStr = originalStr.substring(0, originalStr.length() - 1); } System.out.println("String after removing double quotes = "+originalStr); } }
输出结果
带双引号的字符串=“演示文本”删除双引号后的字符串=演示文本