更加优雅的方式来进行字符串排列的检查和赋值是什么?

huangapple go评论70阅读模式
英文:

What is a more elegant way to do a check on and assign a string permutation?

问题

使用情况:我想检查字符串末尾是否有空格,如果有,将字符串替换为除空格外相同的字符串,并写入日志。我的第一直觉告诉我,以下方式可以实现:

if (someString.Trim() != someString)
{
    someString = someString.Trim();
    Log("whitespaces trimmed");
}

但在这种情况下,我对字符串进行了两次修剪,这对我来说似乎是不必要的。

所以我想到了以下方式:

var trimString = someString.Trim();
if (trimString != someString) 
{
    someString = trimString;
    Log("whitespaces trimmed");
}

然而,这引入了一个基本上无用的变量。它可能会在后面被垃圾回收,但在我看来仍然不太美观。

所以,我考虑使用“using”关键字,但编译器迅速提醒我,字符串不实现IDisposable接口。

所以,我的问题是:是否有更加优雅的方式来编写这种情况的代码?

英文:

The usecase: I want to check if a string has whitespaces at the end, and if it does, replace the string with the same string except for the whitespaces, plus write a log. My first instinct tells me that

if(someString.Trim() != someString)
{
    someString= someString.Trim();
    Log("whitespaces trimmed");
}

would be a way to do so. However, in this case I trim the string twice, which seems unneccessary to me.

So I thought of

 var trimString = someString.Trim();
 if (trimString != someString) 
 {
    someString = trimString;
    Log("whitespaces trimmed");
 }

However, this introduces a basically useless variable. It may get gc'ed later on, but it is still ugly in my opinion. So, I thought of using, well, "using", but the compiler quickly reminded me that string doesnt implement IDisposable.

So, my question is: Is there a more elegant way to write these kinds of scenarios?

答案1

得分: 1

我个人会选择使用 EndsWith

if (someString.EndsWith(" "))
{
    someString = someString.Trim();
    Log("whitespaces trimmed");
}

这将检查字符串的最后一个字符是否是空格(因此需要修剪)。只有在这种情况下,才会修剪字符串的末尾。

节省了两次修剪的开销。

经过一些思考后的编辑

我进一步考虑了这个问题。OP明确提到了_空格_,而不仅仅是空格字符。

以下是检查字符串最后一个字符是否是空白字符的代码:

if (char.IsWhiteSpace(someString[someString.Length - 1]))
{
    someString = someString.Trim();
    Log("whitespaces trimmed");
}

这样可以避免在不需要的情况下多余的 Trim 操作。

英文:

I would personally go with EndsWith:

if (someString.EndsWith(" "))
{
    someString = someString.Trim();
    Log("whitespaces trimmed");
}

This will check whether the last character of the string is a space (hence, it needs to be trimmed). Then, and only then, is the end of the string itself actually trimmed.

Saves on the overhead of trimming twice.

Edit after some thinking time

I had a further think about this. OP mentioned whitespace specifically, not just spaces.

Here's some code that checks for whitespace at the last character of the string:

if (char.IsWhiteSpace(someString[someString.Length - 1]))
{
    someString = someString.Trim();
    Log("whitespaces trimmed");
}

This again saves the overhead of an extra Trim when it may not be needed.

答案2

得分: 0

尝试这个-

string someString = "测试 ";
string val = someString.EndsWith(" ") ? TrimAndLog(someString) : someString;
public string TrimAndLog(string someThing)
{
    someThing = someThing.Trim(); // Trim会删除所有前导和尾随的空格,您应该使用TrimEnd方法
    Log("删除空格");
    return someThing;
}
英文:

Try this-

      string someString = "Test ";
      string val = someString.EndsWith(" ") ? TrimAndLog(someString)  : someString ;
      public string TrimAndLog(string someThing)
      {
            someThing = someThing.Trim(); //Trim will Trim all leading and Trailing whitespaces, You should use method TrimEnd
            Log("whitespaces trimmed");
            return someThing;
      }

huangapple
  • 本文由 发表于 2020年1月6日 16:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定