猿问

如何将替代元素添加到字符串数组

我有一个字符串 - “Hello@World@Good@Morning”


我想将替代词添加到字符串数组中。例如,从上面的字符串中,我应该只将 Hello 和 good 添加到我的新数组中。


for (String s   : StringsTokens) {

            myStringArray = s.split(Pattern.quote("@"));


        }

我如何只向字符串数组添加替代元素。


饮歌长啸
浏览 120回答 2
2回答

萧十郎

试试这个:&nbsp; &nbsp; &nbsp; &nbsp; List<String> result = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; String[] src = "Hello@World@Good@Morning".split( "@" );&nbsp; &nbsp; &nbsp; &nbsp; for ( int i=0 ; i < src.length ; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( i%2 == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add( src[i] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);输出:[你好,好]
随时随地看视频慕课网APP

相关分类

Java
我要回答