如何使用核心Java删除包含*
的行,例如: -
hello how are you.
what are you * doing.
在上面的示例中,我必须删除行中包含what are you * doing
的整行*
。
答案 0 :(得分:1)
String s="hello how are you. \nwhat are you * doing.";
String p[]=s.split("\n");
StringBuffer sb=new StringBuffer();
for(String n:p)
{
if(n.contains("*"))
{
//do nothing
}
else{
sb.append(n);
sb.append("\n");
}
}
System.out.println(sb);
答案 1 :(得分:1)
它会起作用..
String str="hello how are you. \nwhat are you * doing.";
str.replace("*","");
答案 2 :(得分:0)
我是Java的新手,所以如果有什么不对的地方请纠正我,但如果你读出一个文件我会建议:
FileInputStream fstream = new FileInputStream("file.txt");
BufferedReader bf = new BufferedReader(new InputStreamReader(fstream));
String s = bf.readLine();
if(s!=null)
{
if(s.contains("*"))
{
s="";
}
else
{
//work with the rest of your line stored in String
}
}
将其置于循环中(例如for
或while
)
答案 3 :(得分:0)
您可以简单地使用包含的字符串方法。
String s="something *";
if(s.contains(*))
{
s="";
}
else
{
///do else
}
答案 4 :(得分:0)
List<String> originalList = Arrays.asList(
"hello how are you.",
"what are you * doing.");
List<String> filteredList = new ArrayList<>();
for (String item : originalList) {
if (!item.contains("*"))
filteredList.add(item);
}