在 C # 中从字符串中删除最后一个字符。这是一种优雅的方法吗?

我有一个类似于 2223,00的数字字符串。我想把它转换成 2223。这是: 没有信息 之后的“ ,”。假设在“ ,”之后只有两个小数。

我说了:

str = str.Remove(str.Length - 3, 3);

是否有一个更优雅的解决方案? 也许使用另一个函数?-我不喜欢把明确的数字-

165728 次浏览
String.Format("{0:0}", 123.4567);      // "123"

If your initial value is a decimal into a string, you will need to convert

String.Format("{0:0}", double.Parse("3.5", CultureInfo.InvariantCulture))  //3.5

In this example, I choose Invariant culture but you could use the one you want.

I prefer using the Formatting function because you never know if the decimal may contain 2 or 3 leading number in the future.

Edit: You can also use Truncate to remove all after the , or .

Console.WriteLine(Decimal.Truncate(Convert.ToDecimal("3,5")));

Perhaps this:

str = str.Split(",").First();

This will return to you a string excluding everything after the comma

str = str.Substring(0, str.IndexOf(','));

Of course, this assumes your string actually has a comma with decimals. The above code will fail if it doesn't. You'd want to do more checks:

commaPos = str.IndexOf(',');
if(commaPos != -1)
str = str.Substring(0, commaPos)

I'm assuming you're working with a string to begin with. Ideally, if you're working with a number to begin with, like a float or double, you could just cast it to an int, then do myInt.ToString() like:

myInt = (int)double.Parse(myString)

This parses the double using the current culture (here in the US, we use . for decimal points). However, this again assumes that your input string is can be parsed.

You can actually just use the Remove overload that takes one parameter:

str = str.Remove(str.Length - 3);

However, if you're trying to avoid hard coding the length, you can use:

str = str.Remove(str.IndexOf(','));

You can use TrimEnd. It's efficient as well and looks clean.

"Name,".TrimEnd(',');

Use lastIndexOf. Like:

string var = var.lastIndexOf(',');

Use:

public static class StringExtensions
{
/// <summary>
/// Cut End. "12".SubstringFromEnd(1) -> "1"
/// </summary>
public static string SubstringFromEnd(this string value, int startindex)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Substring(0, value.Length - startindex);
}
}

I prefer an extension method here for two reasons:

  1. I can chain it with Substring. Example: f1.Substring(directorypathLength).SubstringFromEnd(1)
  2. Speed.

Try the following. It worked for me:

str = str.Split(',').Last();

You could use LastIndexOf and Substring combined to get all characters to the left of the last index of the comma within the sting.

string var = var.Substring(0, var.LastIndexOf(','));

Since C# 8.0 it has been possible to do this with a range operator.

string textValue = "2223,00";
textValue = textValue[0..^3];
Console.WriteLine(textValue);

This would output the string 2223.

The 0 says that it should start from the zeroth position in the string

The .. says that it should take the range between the operands on either side

The ^ says that it should take the operand relative to the end of the sequence

The 3 says that it should end from the third position in the string