I'm trying to trim out the last charater from a string if not a number For example : "2+3-4*" From there last astrik needs to be trimmed as it is not a number my result should be as "2+3-4". If user enters "2+3+8" then no needs to trim as the last is number.
-
1I don't think that's exactly a dupe, just highly related. OP doesn't necessarily know what character they are looking for to trim, so it does involve a little more intelligence than just trimming a specific character. – Broots Waymb Feb 20 '17 at 18:14
-
1I have to agree with @DangerZone here - it's not a dupe of that question. – EJoshuaS - Stand with Ukraine Feb 20 '17 at 18:30
-
1Also, it seems like there is a disagreement among answers. What if there are multiple trailing non-numerics? (for example, `"2+3-4**"`). Should both `*` be removed, or only the very last? – Broots Waymb Feb 20 '17 at 18:37
-
@DangerZone That's it, it seems like the OP ignores this case. Probably the OP will come again in a while once has realized that the selected answer has this issue... – Matías Fidemraizer Feb 20 '17 at 20:38
-
Possible duplicate of [Trim last character from a string](http://stackoverflow.com/questions/3573284/trim-last-character-from-a-string) – Gerard de Visser Feb 21 '17 at 08:06
10 Answers
You can use this regex: [^0-9]$
to determine if the last character is something other than a number. If the regex is a match, simply remove the last character.
string a = "abc";
if (Regex.IsMatch(a, "[^0-9]$"))
{
a = a.Remove(a.Length - 1);
}

- 11,977
- 56
- 49
- 78
-
Why this gets more love when it just drops one character and, once you've matched what you want, what's the point of not using a regex replace... I would understand, as other answerers, that it can be solved **without** regexps... but why mixing both approaches? Anyway, it drops only a non-numeric character... – Matías Fidemraizer Feb 20 '17 at 18:33
-
2@MatíasFidemraizer - I don't understand your last sentence. It's only supposed to drop a non-numeric character according to the question. – Broots Waymb Feb 20 '17 at 18:34
-
-
@MatíasFidemraizer The OP specified that he only wants to drop a single character. Besides, you can make this match multiple characters if you want. – EJoshuaS - Stand with Ukraine Feb 20 '17 at 22:31
You can check the last char is numeric or not using below snippet:
eg:
string value = "4+8-4*";
int outInt = 0;
bool isLastCharNumeric = int.TryParse(value[value.Length - 1].ToString(), out outInt);
if (!isLastCharNumeric)
{
//chop off the last char
value = value.Remove(value.Length - 1;);
}

- 331
- 3
- 15
-
@VenkateshKonduru - Just note that this will not work if you need to remove multiple non-numeric characters. This will only remove the last. – Broots Waymb Feb 20 '17 at 20:41
-
Correct however your question did state, "I'm trying to trim out the last charater from a string if not a number" – SashaStojanovic Feb 20 '17 at 20:45
You could grab the last character and check if it is a number then remove if so.
string str = "2+3-4*";
if(!Char.IsNumber(str[str.Length - 1])){
str = str.Remove(str.Length - 1);
}

- 5,826
- 6
- 42
- 80
Probably you can use a regular expression:
string input = "2+3+";
// Regexp => one or more occurences of a non-numeric character at
// the end of the string
string santizedInput = Regex.Replace(input, "[^0-9]+$", "");

- 63,804
- 18
- 124
- 206
I would use a regular expression. [^\d]+$
See the results: https://regex101.com/r/pE0h48/1

- 1,369
- 10
- 27
I think regex would be a good clean way to address this.
Regex rgx = new Regex("\D+$");
string result = rgx.Replace("2+3-4*", "");

- 1,829
- 13
- 15
My approach would be using a regular expression, but instead of using it to replace the last character, I would use the regular expression to extract the valid part of the string. My approach is coded in Java, but you can very easily translate it to C#.
Pattern pattern = Pattern.compile("([0-9]+)([\\+\\-\\:\\*][0-9])+");
Matcher m;
String input = "++2+4+5-6*3+4*2-3--";
m = pattern.matcher(input);
while(m.find()){
System.out.println(m.group());
}

- 1,919
- 11
- 21
I'm not big on regular expressions as they're pretty slow, relatively speaking. We can solve this pretty easily by defining what numbers are and then just checking to see if the last character is one of them. So first lets open by specifying what we consider to be numbers.
static readonly char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
Now we need to return one of two results depending on whether or not our last character is one of the characters we just listed. To accomplish this lets use a ternary statement.
var testInput = "testing5";//Testing input
var trimmedString =
numbers.Any(x => x == testInput[testInput.Length - 1]) ?//testInput.Length - 1 is going to give us the last character in the string
testInput : //It's a number so go ahead and return the full string
testInput.SubString(0, testInput.Length - 1);//It's not a number so lets return all but the last character in the string as a new string.
So our final product looks like this. I encapsulated it into a method since it's a pretty self contained aspect of your code (presumably).
static readonly char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public static string TrimNonNumbers(string input)
{
if (string.IsNullOrEmpty(input)) return input;//Our math will produce an index out of range exception with an empty string. So this is a safety to prevent that.
return numbers.Any(x => x == testInput[testInput.Length - 1]) ? testInput : testInput.SubString(0, testInput.Length - 1);
}

- 1,079
- 1
- 12
- 27
Working, tested code
using System;
public class Program
{
public static void Main()
{
string calculation = "2+3+81%";
int length = calculation.Length;
if(!Char.IsNumber(calculation[length-1])){
calculation = calculation.TrimEnd(calculation[length-1]);
}
Console.Write(calculation);
}
}

- 612
- 1
- 12
- 32
-
2Why would you want to check the character in a position greater than the length of the string itself? This will cause an exception to be thrown every time. – Broots Waymb Feb 20 '17 at 18:21
-
i got something mixed up there, its untested pseudocode tough. fixed it. – Master Azazel Feb 20 '17 at 22:25