gpt4 book ai didi

java - 从字符串 [] 的所有元素中删除引号

转载 作者:行者123 更新时间:2023-11-29 04:38:21 25 4
gpt4 key购买 nike

我得到了一个文本文档,其中有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com