猿问

Java 中的 ArrayList<String>,[€14,€55,€500]

我有一个存储在ArrayList字符串中的 webElement 列表,如下所示:

ArrayList<String> prints out [€300, €34, €56]

我如何删除€符号?。我已经尝试过remove()其他方法,但它们不起作用。它ArrayList虽然。


守着星空守着你
浏览 136回答 3
3回答

心有法竹

只需遍历您的列表并使用 String::replaceAll&nbsp; &nbsp; List<String> list = new ArrayList <String>();&nbsp; &nbsp; list.add("$123");&nbsp; &nbsp; list.add("$345");&nbsp; &nbsp; list.add("12345");&nbsp; &nbsp; for (String s : list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s.replaceAll("^\\$", ""));&nbsp; &nbsp; }如果你想更新List你可以使用的值List::set

慕的地6264312

目前还不清楚你想要什么。一个带有修剪元素的新列表,只打印没有欧元符号或其他东西的元素?欧元在任何元素的开头,是否可以多次出现?等等......这里只是一个想法:List<String> list = Arrays.asList("€14", "€55", "€500");System.out.println(list);List<String> trimmedList =&nbsp; &nbsp; list.&nbsp; &nbsp; &nbsp; &nbsp; stream().&nbsp; &nbsp; &nbsp; &nbsp; map(s -> s.startsWith("€") ? s.substring(1) : s).&nbsp; &nbsp; &nbsp; &nbsp; collect(Collectors.toList());System.out.println(trimmedList);它打印:[€14, €55, €500][14, 55, 500]

一只斗牛犬

你也可以使用这个正则表达式,&nbsp; List<String> myList = new ArrayList<String>();&nbsp; myList.add("$14");&nbsp; myList.add("$55");&nbsp; myList.add("$500");&nbsp; System.out.println(myList);&nbsp; &nbsp;for (int i = 0; i < myList.size(); i++) {&nbsp; &nbsp; &nbsp;myList.set(i, myList.get(i).replaceAll("^[^\\d]+", ""));&nbsp; }&nbsp; System.out.println(myList);
随时随地看视频慕课网APP

相关分类

Java
我要回答