Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why trim is not working?

Tags:

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 ?

Code:

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

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

Updated

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;
    }

Thanks.

like image 581
Rachel Avatar asked Jan 18 '11 20:01

Rachel


People also ask

Why trim function is not working?

One space character commonly used in Web pages that TRIM() will not remove is the non-breaking space. If you have imported or copied data from Web pages you may not be able to remove the extra spaces with the TRIM() function if they are created by non-breaking spaces.

Why is trim not removing spaces in Excel?

The TRIM function can get rid of white spaces, but it cannot eliminate non-printing characters. Technically, Excel TRIM is designed to only delete value 32 in the 7-bit ASCII system, which is the space character. To remove spaces and non-printing characters in a string, use TRIM in combination with the CLEAN function.

Does trim get rid of spaces?

Video: TRIM FunctionThe Excel TRIM function removes all the spaces in a text string, except for single spaces between words. In this video you'll see how to use the TRIM function to: remove spaces from the start and end of a text string. remove all except single spaces between words.


2 Answers

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();

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


Original answer:

Just use this.poNumber = poNumber.trim();

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.

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

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.

like image 55
Bozho Avatar answered Oct 10 '22 18:10

Bozho


Are you sure you're reading the right variable for your output? You have 'poNumber', which is the original untrimmed string, and 'this.poNumber', which would get the trimmed string.

like image 35
Phil Avatar answered Oct 10 '22 17:10

Phil