-2

I have a string oCustomerOrderNumber which I am passing a value into iCustomerOrderNumber. I would like to remove the last 3 characters from this iCustomerOrderNumber but unable to get this to work. Error I get is that it does not like the -.

Code:

 oCustomerOrderNumber = iCustomerOrderNumber.trim() -  
 (iCustomerOrderNumber.length() -3);

Example. ICustomerOrderNumber is 1234567 I want oCustomerOrderNumber to be 1234.

4
  • 5
    you should use the substring method. trim() doesn't return a length, but a trimmed version of a String Commented Dec 4, 2018 at 14:02
  • yourString.substring(0, yourString.length() - 3). Commented Dec 4, 2018 at 14:02
  • There is no minus operator for strings in Java Commented Dec 4, 2018 at 14:02
  • What should I use instead of -? Commented Dec 4, 2018 at 15:11

2 Answers 2

3

For getting a portion of a string you can use substring method and here you can define from where until where you want to fetch from any String:

iCustomerOrderNumber.substring(0, iCustomerOrderNumber.length()-3);
Sign up to request clarification or add additional context in comments.

3 Comments

you should at least provide some explanation of what you do, why you do it, and what the difference with the original code is
@Stultuske yeah it was a fast send. hehe :-)
And all in all you can not remove anything from String, since String is immutable class, but you can create a new modified string from existed one, as Mehdi showed.
-4

oCustomerOrderNumber = iCustomerOrderNumber.substring(0, (iCustomerOrderNumber.length() -3));

1 Comment

This is nothing but a copy-paste of a (part of a) previously posted answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.