根据字符串值修剪java字符串?
我有一个数据库查询,它返回列中的一些字符串数据。我想根据数据中是否存在特定字符串来修剪此字段?
如果字符串包含逗号“,”,我希望返回第一个逗号的左侧 如果字符串包含连字符“-”,我想返回第一个连字符的左侧 如果字符串两者都不包含,我想返回字符串的前 14 个字符?
我现在正朝着这个方向前进:
StringHandling.LEFT(row1.DESCRIPTION,StringHandling.INDEX(row1.DESCRIPTION,"-"))
如何包含一些逻辑来检查逗号或缺少逗号并在表达式中返回适当的子字符串?
非常感谢!
I have a database query that returns some string data in a column. I would like to trim this field based on whether a particular string exists in the data?
If the string contains a comma ",", I'm looking to return LEFT of that first comma
If the string contains a hypen "-", I would like to return LEFT of that first hypen
If the string contains neither, I would like to return the first 14 characters of the string?
I am headed in this direction at the moment:
StringHandling.LEFT(row1.DESCRIPTION,StringHandling.INDEX(row1.DESCRIPTION,"-"))
How can I include some logic to check for the comma or lack thereof and return the appropriate substring in my expression??
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种选择(我偷了 Bohemian 的 Math.min() 技术;-)):
Another alternative (I stole Bohemian's Math.min() technique ;-)) :
像这样的东西吗? (编辑为处理
null
)Math.min()
用于处理字符串长度小于 14 个字符的情况。Something like this? (Edited to handle
null
)The
Math.min()
is to handle when the string is less than 14 characters long.检查 API 中的 String.contains、String.indexOf 和 String.substring。
Check the API for String.contains, String.indexOf, and String.substring.