# Strip Letters From End of String

Steve Dyke
Ranch Hand
Posts: 1695
1
What is is best way to strip letters from the end of a string? Example: 854965-401AB

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
Strings are immutable, so you can't.

You can, however, create a new string that is what you want, but you have to define your conditions better.

Can there be letters in the middle of the string? will there always be exactly 2 letters? Will the string always be the same length?

How you answer these would effect how I would approach the problem, and may spark additional questions.

providing a single example does NOT define the problem. Please give more details.

Steve Dyke
Ranch Hand
Posts: 1695
1
fred rosenberger wrote:Strings are immutable, so you can't.

You can, however, create a new string that is what you want, but you have to define your conditions better.

Can there be letters in the middle of the string? will there always be exactly 2 letters? Will the string always be the same length?

How you answer these would effect how I would approach the problem, and may spark additional questions.

providing a single example does NOT define the problem. Please give more details.

Yes there can be letters in the midst of the string I want to leave as is.
No there will not always be exactly 2 letters.
No the string will not always be the same length.

Somehow I thought I could loop through the string from the end backwards, check to see if character is a digit, it it is not then strip that character off. When a digit is encountered stop the loop. I just don't know if this is the correct approach nor how to write the code.

Henry Wong
author
Marshal
Posts: 21716
85

One option is to use the replaceAll() method -- just replace letters at the end of the string with nothing.

Henry

Steve Dyke
Ranch Hand
Posts: 1695
1
Henry Wong wrote:
One option is to use the replaceAll() method -- just replace letters at the end of the string with nothing.

Henry

But how do I identify the letters at the end of the string only?

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
If you have no way of knowing how many letters (if any) will be at the end of the string, then yes, you do have to start at the end and work your way backwards.

There MAY be a way to use a regex to find the last occurrence of 1 or more letters, but you have to be careful that the one you find is not in the middle...i.e. 1234-ABC7

From what I understand, you would NOT want to replace these.

What I'd probably do it what you suggest, except not strip them letter by letter. All you need to do is find the POSITION of the last character you want, then take a sub-string. Very sloppy pseudo-code:

Of course, there are probably much better ways, but I'm lazy and this isn't too hard to do...

Shanky Sohar
Ranch Hand
Posts: 1051
What is is best way to strip letters from the end of a string? Example: 854965-401AB

one thing,the string are immutable,,you cannot strip it............but you can extract the specified characters from a string......

for example you wanna extract from first charater to third last charater..........so your new string will be 854965-40

you can do this
String s1=854965-401AB;
String s2=s1.substringg(indexstart,indexend);
so

String s2=s1.substringg(0,9);
this will produce a new string s2....................that will be 854965-40;

but it is not possible for you 2 make changes in s1..

Henry Wong
author
Marshal
Posts: 21716
85
Steve Dyke wrote:
But how do I identify the letters at the end of the string only?

Letters at the end of the string, can be found with a regex that includes one or more letters followed by the end of line boundary.

Henry

Shanky Sohar
Ranch Hand
Posts: 1051
Henry Wong wrote:
Steve Dyke wrote:
But how do I identify the letters at the end of the string only?

Letters at the end of the string, can be found with a regex that includes one or more letters followed by the end of line boundary.

Henry

there is a simple way to identify the letters at the end of string...........
For example,you want to get the string but you donot want the last two characters...

simple way
String s2=s1.subString(0,(s1.length()-2));

Charles Bradley
Ranch Hand
Posts: 32
I agree, you should figure out how many characters to remove and then make a new string by omitting the correct number of terminal characters. Be careful about the criterion for what should be removed. Do you want to omit letters only, or do you perhaps want to omit everything except digits. Methods in Class Character, such as isDigit() and isLetter(), can be a help. Also, be aware of limiting cases. Will your code work if there is only digits? If there is only letters? If the string is empty?