简体   繁体   English

Java - 如何搜索字符串数组以删除所有引号?

[英]Java - how do you search through an array of strings to remove all the quotation marks?

Let's say, for example, I have an array of 5 elements, each of them being a string. 比方说,我有一个包含5个元素的数组,每个元素都是一个字符串。 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 不是数组中的整个字符串

Remove all quotes from a string: 从字符串中删除所有引号:

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

http://ideone.com/RFZZq http://ideone.com/RFZZq

Because the String would be in array, you would have to loop through the entire array, and for each element, check to see if quotations marks were found. 因为String将在数组中,所以您必须遍历整个数组,并且对于每个元素,检查是否找到了引号。 If they were found, they will be removed by using String's replace method. 如果找到它们,将使用String's replace方法删除它们。 Example below: 示例如下:

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("\"", "");
  }
}

Above mentioned ways are good but you can also do that through this code 上面提到的方法很好,但您也可以通过此代码执行此操作

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

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

It will also remove all the quotes from a string 它还将删除字符串中的所有引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM