修改String ArrayList中的特定字符

时间:2016-05-03 20:15:31

标签: java string arraylist replace

所以我们有以下内容:

ArrayList<String> list = ( new ArrayList<String>( ) );

list.add( new String( "hello" ) ); // add first word at position [0]
list.add( new String( "world" ) ); // add second word at position [1]

我不想使用.replace方法,因为它会替换所有出现的事件。假设我只想修改hello字中的第一个“l”并将其改为“x”,我该怎么做?我只想在数组中的字符串中定位特定元素#。

System.out.println( list ); // display the full arraylist

初始输出:

[hello, world]

期望的输出:

[hexlo, world]

1 个答案:

答案 0 :(得分:0)

例如,您可以使用list.set()方法,如下所示:

list.set(index, list.get(index).modifyingStringMethodhere());

根据您的需要修改字符串非常简单,有java本地支持的大量工具。

相关问题