给了我一个文本文件,该文件的大写字母均为5K +,并且全都用“双引号”引起。我用逗号将所有名称分开,所以我有一个填充的String [],用引号将名称填充。我需要删除名称中的所有引号,但我尝试使用.trim('“'),但始终会出错。
这是我的代码:
public class NameScore {
public static void main(String[] args) throws IOException {
//File path to name list
String NAME_LIST = "/Users/BR/NameScores/src/p022_names.txt";
//imports the file with names
Scanner input = new Scanner(new File(NAME_LIST));
//Array list that holds the appx. 5K names. Each name split by a comma
String[] nameList = (input.nextLine()).split(",");
//puts the list into alphabetical order.
Arrays.sort(nameList);
for (int i = 0; i < nameList.length; i++) {
//nameList[i] = nameList[i].trim('"');
System.out.println(nameList[i]);
}
}
}
最佳答案
您可以对String.replaceAll(String, String)
使用正则表达式,例如
System.out.println(nameList[i].replaceAll("\"", ""));
关于java - 从字符串的所有元素中删除引号[],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40296534/