简体   繁体   English

Java:如何从文本文件中删除具有相同前缀的字符串?

[英]Java: How to remove strings having the same prefix from a text file?

I need to replace some string from a text file such as "upcoming:event=123982". 我需要替换文本文件中的一些字符串,例如“ upcoming:event = 123982”。

The strings always start with "upcoming:event=". 字符串始终以“ upcoming:event =“开头。 How can I remove them (including the digits) ? 如何删除它们(包括数字)? (I'm processing the text with Java). (我正在用Java处理文本)。

Should I use wildcards ? 我应该使用通配符吗? something like "upcoming:event=*" ? 类似于“ upcoming:event = *”?

thanks 谢谢

String str = "upcoming:event=123982";
System.out.println(str.replaceFirst("upcoming:event=[0-9]*", "changed"));

Output: changed 输出:已更改

 String s="upcoming:event=123982;upcoming:event=100;upcoming:event=200;upcoming:event=900;upcoming:event=1987";
System.out.println(s.replaceAll("upcoming:event=[0-9]*", ""));

Output: 输出:

123982;100;200;900;1987

Even though this is probably is not a good way you could 即使这可能不是您可以使用的好方法

String text = "upcoming:event=*YOUR TEXT";      
System.out.println(text.replace("upcoming:event=*", ""));

Result will be YOUR TEXT in this case... 在这种情况下,结果将是您的文字...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将同一文本文件中的整数和字符串都添加到不同的数组中 - How to add both ints and strings from the same text file into different arrays Java Java - 如何读取同一行中包含 Int 和字符串的文本文件 - Java - How to read a text file with Int's and Strings in the same line 如何从字符串映射Java中删除/剥离唯一文本 - How to remove/stripp off unique text from a Map of Strings Java Java:从超大型文本文件中读取具有相同前缀的行组 - Java: read groups of lines with same prefix from very large text file 如果在 java 中具有 # 作为前缀,则从逗号分隔的字符串中删除并获取文本? - Remove and get text, from comma separated string if it has # as prefix in java? 如何使用Java从文本文件中的指定索引提取字符串? - How to extract strings from specified indices in text file using Java? 如何对 Java 中的文本文件中的引用字符串进行排序 - How to sort quoted strings from a text file in Java 用Java读取文本文件后,如何将读取文本文件的函数的读取内容传递给同一类中的其他函数? - Having read a text file in java, how do i pass the read content from the function that reads the text file to some other function in the same class? 如何从文本中删除字符串? - How can I remove strings from text? 从文本文件中读取Java(字符串和整数) - Java Reading from a text file (Strings and integers)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM