修剪最后一个字符,如果不是数字

时间:2017-02-20 18:06:29

标签: c#

我试图从字符串中删除最后一个字符,如果不是数字例如:“2 + 3-4 *”从那里最后astrik需要修剪,因为它不是数字我的结果应该是“2 + 3-4”。如果用户输入“2 + 3 + 8”,则不需要修剪,因为最后一个是数字。

10 个答案:

答案 0 :(得分:6)

您可以使用此正则表达式:[^0-9]$来确定最后一个字符是其他而不是数字。如果正则表达式匹配,只需删除最后一个字符。

string a = "abc";
if (Regex.IsMatch(a, "[^0-9]$"))
{
  a = a.Remove(a.Length - 1);
}

答案 1 :(得分:5)

您可以使用以下代码段检查最后一个字母是否为数字:

例如:

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

答案 2 :(得分:2)

你可以抓住最后一个字符并检查它是否是一个数字然后删除,如果是的话。

string str = "2+3-4*";

if(!Char.IsNumber(str[str.Length - 1])){
  str = str.Remove(str.Length - 1);
}

答案 3 :(得分:1)

使用正则表达式:

string str= "2+3-4*";
str = Regex.Replace(str, "[^0-9]$", "");

答案 4 :(得分:0)

可能你可以使用正则表达式:

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]+$", "");

答案 5 :(得分:0)

我会使用正则表达式。 [^ \ d] + $

查看结果:https://regex101.com/r/pE0h48/1

答案 6 :(得分:0)

我认为正则表达式是一种很好的解决方法。

Regex rgx = new Regex("\D+$");
string result = rgx.Replace("2+3-4*", "");

答案 7 :(得分:0)

我的方法是使用正则表达式,但不使用它来替换最后一个字符,我会使用正则表达式来提取字符串的有效部分。我的方法是用Java编写的,但您可以很容易地将其转换为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());
}

答案 8 :(得分:0)

我对正则表达式并不大,因为相对而言,它们相当慢。我们可以通过定义数字来轻松解决这个问题,然后检查最后一个字符是否是其中之一。首先,通过指定我们认为是数字的内容来打开。

static readonly char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

现在我们需要返回两个结果中的一个,具体取决于我们的最后一个字符是否是我们刚刚列出的字符之一。要完成此操作,请使用三元语句。

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.

所以我们的最终产品看起来像这样。我将它封装到一个方法中,因为它是代码的一个非常独立的方面(大概)。

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

答案 9 :(得分:-2)

工作,测试过的代码

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