根据字符串值修剪java字符串?

发布于 2024-11-19 05:20:25 字数 333 浏览 5 评论 0原文

我有一个数据库查询,它返回列中的一些字符串数据。我想根据数据中是否存在特定字符串来修剪此字段?

如果字符串包含逗号“,”,我希望返回第一个逗号的左侧 如果字符串包含连字符“-”,我想返回第一个连字符的左侧 如果字符串两者都不包含,我想返回字符串的前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-11-26 05:20:25

另一种选择(我偷了 Bohemian 的 Math.min() 技术;-)):

 public String dbString(String s){
        String[] parts = s.split("(,|-)");

        String trimmed = parts.length > 1? parts[0] : parts[0].substring(0, Math.min(14,  parts[0].length());

        return trimmed;
    }

Another alternative (I stole Bohemian's Math.min() technique ;-)) :

 public String dbString(String s){
        String[] parts = s.split("(,|-)");

        String trimmed = parts.length > 1? parts[0] : parts[0].substring(0, Math.min(14,  parts[0].length());

        return trimmed;
    }
世界如花海般美丽 2024-11-26 05:20:25

像这样的东西吗? (编辑为处理 null

public static String specialTrim(String input) {
    if (input == null)
        return null; // or return "", or whatever else you want
    if (input.contains(","))
        return input.substring(0, input.indexOf(","));
    if (input.contains("."))
        return input.substring(0, input.indexOf("."));
    return input.substring(0, Math.min(14, input.length()));
}

Math.min() 用于处理字符串长度小于 14 个字符的情况。

Something like this? (Edited to handle null)

public static String specialTrim(String input) {
    if (input == null)
        return null; // or return "", or whatever else you want
    if (input.contains(","))
        return input.substring(0, input.indexOf(","));
    if (input.contains("."))
        return input.substring(0, input.indexOf("."));
    return input.substring(0, Math.min(14, input.length()));
}

The Math.min() is to handle when the string is less than 14 characters long.

樱娆 2024-11-26 05:20:25

检查 API 中的 String.contains、String.indexOf 和 String.substring。

String theString = <wherever it comes from>;
// if it contains a ,
if (theString.contains(",")) {
    // return all elements one position to the left
    return theString.substring(0, theString.indexOf(",") - 1);
} else // and repeat

Check the API for String.contains, String.indexOf, and String.substring.

String theString = <wherever it comes from>;
// if it contains a ,
if (theString.contains(",")) {
    // return all elements one position to the left
    return theString.substring(0, theString.indexOf(",") - 1);
} else // and repeat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文