import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
public class ListUse
{
@Test
public void test()
throws Exception
{
// 准备实验的数据
List<String> source = new ArrayList<String>();
for (int i = 0; i <= 10; i++)
{
source.add(i + "|");
}
Iterator<String> it = source.iterator();
// 打印出来
StringBuffer stringBuffer = new StringBuffer();
while (it.hasNext())
{
stringBuffer.append(it.next());
}
System.out.println("这是准备的list,打印出来是:\n" + stringBuffer.toString());
if (isItEquals(source, "|"))
{
List<String> result = convert(source, "|", "");
System.out.println("这是转换之后的list,打印出来是:\n" + result.toString());
}
}
// 去掉集合中的 |
public static List<String> convert(List<String> lists, String sourceStr, String targetStr)
throws Exception
{
// 判断入参是否为空
for (int i = 0; i < lists.size(); i++)
{
lists.set(i, lists.get(i).replace(sourceStr, targetStr));
}
return lists;
}
// 判断是否包含 |,如果不包含,直接返回
public static boolean isItEquals(List<?> list, String str)
throws Exception
{
// 判断入参是否为空
boolean isItEquals = false;
if (null == list)
{
// do ...
}
else
{
Iterator<?> it = list.iterator();
while (it.hasNext())
{
String itStr = (String)it.next();
if (itStr.contains(str))
{
isItEquals = true;
}
}
}
return isItEquals;
}
}
替换集合中的 某个符号
最新推荐文章于 2021-03-01 15:15:39 发布