为什么修剪不起作用

为什么修剪不起作用

本文介绍了为什么修剪不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修剪字符串中的前导空格,我不知道我的方法有什么问题,任何建议都会受到赞赏吗?

I am trying to trim leading whitespaces from the string and I do not know what is wrong with my approach, any suggestions would be appreciated ?

代码:

this.poNumber = poNumber.equals()? poNumber:poNumber.trim();

我正在从csv文件中读取poNumber为IG078565和IG083060并且输出也得到相同的值相同的空格,不确定原因?

am reading poNumber from csv file as "     IG078565 and IG083060 " and output also am getting same value with same whitespaces, not sure why ?

更新

添加完整的方法以获得更好的效果上下文:

Adding complete method for better context:

public BillingDTO(String currency, String migrationId, String chargeId, String priceId, String poNumber, String otc,
            String billingClassId, String laborOnly) {
        super();
        this.currency = currency.equals("") ? currency : currency.trim();
        this.migrationId = migrationId.equals("") ? migrationId : migrationId.trim();
        this.chargeId = chargeId.equals("") ? chargeId : chargeId.trim();
        this.priceId = priceId.equals("") ? priceId : priceId.trim();
        this.poNumber = poNumber.equals("") ? poNumber : poNumber.trim();
            //poNumber.trim();
        //System.out.println("poNumber:"+this.poNumber.trim());
        //this.poNumber = poNumber.equals("") ? poNumber : poNumber.trim();
        this.otc = otc.equals("") ? otc : otc.trim();
        this.billingClassId = billingClassId.equals("") ? billingClassId : billingClassId.trim();
        this.laborOnly = laborOnly.equals("") ? "N" : laborOnly;
    }

谢谢。

推荐答案

更新您的空白区域似乎不是空格(ascii = 32)。你的代码是160,这是一个不间断的空间。 trim()无法处理它。所以你必须这样做:

Update It appears your whitespace is not a whitespace (ascii=32). Yours is with code 160, which is a no-breaking space. trim() does not handle it. So you must do something like this:

this.poNumber = poNumber.replace(String.valueOf((char) 160), " ").trim();

你最好创建一个实用程序 - YourStringUtils.trim(string)并执行两项操作 - .trim()替换(..)

You'd better create an utility - YourStringUtils.trim(string) and there perform the two operations - both .trim() and the replace(..)

原始答案:

只需使用 this.poNumber = poNumber.trim();

如果有可能 poNumber null ,那么你可以使用null-safe this.poNumber = StringUtils.trim (poNumber); 来自。

If there is a possibility for poNumber to be null, then you can use the null-safe this.poNumber = StringUtils.trim(poNumber); from commons-lang.

如果你想要 trimToEmpty(..) > null 要转换为空字符串。

You can also use trimToEmpty(..) from the same class, if you want null to be transformed to an empty string.

如果你不想依赖commons-lang,那么只需添加一个if -clause:

If you don't want to rely on commons-lang, then just add an if-clause:

if (poNumber != null) {
    this.poNumber = poNumber.trim();
}

正如问题评论中所述 - 确保您正在检查权利修剪后变量。您应该检查实例变量。你的参数(或局部变量,我无法分辨)不会改变,因为字符串是不可变的。

As noted in the comments under the question - make sure you are checking the right variable after the trimming. You should check the instance variable. Your parameter (or local variable, I can't tell) does not change, because strings are immutable.

这篇关于为什么修剪不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:10