删除嵌入字符串(Delete embedded string)

编程入门 行业动态 更新时间:2024-10-05 05:25:25
删除嵌入字符串(Delete embedded string)

我正在寻找能够让我“删除嵌入字符串”的最有效的Java方法。 例如,如果我有字符串"Bob said \"Hello world!\"." ,我打电话给它的方法,结果应该是: "Bob said ." 。 帮助将不胜感激!

请注意,我希望这种方法是通用的 ,所以如果Bob说“hello world”之外的东西,它仍然可以工作。

I'm looking for the most efficient Java method that would allow me to 'delete an embedded string'. For example, if i had the string "Bob said \"Hello world!\".", and I call the method on it, the result should be: "Bob said .". Help would be appreciated!

Note that I want this method to be general, so that if Bob says something besides "hello world" it would still work.

最满意答案

你可以试试

String string = "Bob said \"Hello world!\"."; string = string.replaceAll("\"[^\"]+\"", "");

如果您只想删除第一个引用的文本,请使用replaceFirst。

You can try

String string = "Bob said \"Hello world!\"."; string = string.replaceAll("\"[^\"]+\"", "");

Use replaceFirst if you only want to remove the first quoted text.

更多推荐

本文发布于:2023-08-04 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Delete   string   embedded

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!