-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.

George H
  • 31
  • 6

2 Answers2

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);
Mehdi
  • 3,795
  • 3
  • 36
  • 65
  • 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 Dec 04 '18 at 14:04
  • @Stultuske yeah it was a fast send. hehe :-) – Mehdi Dec 04 '18 at 14:04
  • 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. – Pasha Dec 04 '18 at 14:07
-4

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

CR Sardar
  • 921
  • 2
  • 17
  • 32