1

Let's say, for example, I have an array of 5 elements, each of them being a string. One of the strings, however, has quotation marks around it, which I would like to remove automatically. Is there a function for doing this easily?

*remove the quotation marks, that is. Not the entire string from the array

4

3 回答 3

4

Remove all quotes from a string:

String input = "foo \"bar\" baz";
String output = input.replace("\"", "");

http://ideone.com/RFZZq

于 2012-04-22T05:09:21.653 回答
3

因为String将在数组中,所以您必须遍历整个数组,并且对于每个元素,检查是否找到引号。如果被发现,将通过使用String's replace方法将其删除。下面的例子:

String[] myArray = {"This", "may", "have", "a", "\"Quotation\"", "In", "It"};
for (int i = 0; i < myArray.length; i++) {
  if (myArray[i].contains("\"")) {
      myArray[i] = myArray[i].replace("\"", "");
  }
}
于 2012-04-22T05:15:52.633 回答
-3

上面提到的方法很好,但你也可以通过这段代码做到这一点

      String s1 = "bhb/vgvg/vvv";
      String fr[] = s1.split("/");

      for (String string : fr) 
      {
        System.out.print(string);
      }

它还将删除字符串中的所有引号

于 2012-04-22T05:39:54.400 回答