如何在两个分隔符之间提取字符串 [英] How to extract a string between two delimiters

查看:196
本文介绍了如何在两个分隔符之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

两个分隔符之间的子字符串

我有一个类似

I have a string like


ABC [这是提取]

"ABC[ This is to extract ]"

我想提取部分这是在java中提取。我试图使用拆分,但它不按我想要的方式工作。有没有人有建议?

I want to extract the part "This is to extract" in java. I am trying to use split, but it is not working the way I want. Does anyone have suggestion?

推荐答案

如果你只有一对括号( [] )在你的字符串中,你可以使用 indexOf()

If you have just a pair of brackets ( [] ) in your string, you can use indexOf():

String str = "ABC[ This is the text to be extracted ]";    
String result = str.substring(str.indexOf("[") + 1, str.indexOf("]"));

这篇关于如何在两个分隔符之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆