开发者

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

开发者 https://www.devze.com 2023-02-12 08:24 出处:网络
I need to replace some string from a text file such as \"upcoming:event=123982\". The strings always start with \"upcoming:event=\". How can I remove them (including the digi开发者_如何学编程ts) ? (I

I need to replace some string from a text file such as "upcoming:event=123982".

The strings always start with "upcoming:event=". How can I remove them (including the digi开发者_如何学编程ts) ? (I'm processing the text with Java).

Should I use wildcards ? something like "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


Use String.startsWith();


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消