0

I have a string _outputUrl and I want to check whether its last character is "&" or not. And if its "&" I want to remove it from string. I have made this with If case like this

if (_outputUrl!=null && _outputUrl[_outputUrl.Length - 1].ToString() == "&")
{
    _outputUrl = _outputUrl.Remove(_outputUrl.Length - 1);

}

How can I convert this to a linq expression?

None
  • 5,582
  • 21
  • 85
  • 170
  • 4
    Any reason you want to use Linq when you can use [TrimEnd](http://msdn.microsoft.com/en-us/library/system.string.trimend(v=vs.110).aspx)? – CodingIntrigue Jan 22 '14 at 10:01
  • 1
    The `.ToString` is unnecessary. (use `_outputUrl[_outputUrl.Length - 1] == '&'`, i.e. a char literal instead of a string literal) – CodesInChaos Jan 22 '14 at 10:02
  • I think the thing you're going to hit here is that you're using the wrong tool for the job. Could you do it in LINQ? Sure. Should you ever do it in real code? No, it's hideously inefficient. If you insist on using LINQ against individual characters in your array, look into `string.ToCharArray()` - then it's just a list of char you can manipulate with `LINQ` – Basic Jan 22 '14 at 10:05
  • LINQ means "Language Integrated Query". How is what you're asking even possible as a query. I think you're asking if there's an extension method to do what you want, not LINQ. And `TrimEnd` is it. – Enigmativity Jan 22 '14 at 10:06
  • Maybe the actual problem the OP tries to solve is rather "[How to build a query string](http://stackoverflow.com/q/829080/205233)". – Filburt Jan 22 '14 at 11:01

2 Answers2

4

If you have only one & at the end (or want to trim more), you can use TrimEnd:

_outputUrl = _outputUrl.TrimEnd('&');

if you have more than one & at the end and want to remove just one:

_outputUrl = _outputUrl.EndsWith("&")?
                _outputUrl.Substring(0, _outputUrl.Length-1):
                _outputUrl;
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
0

LINQ is a great tool but wrong for this kind if problem. LINQ acts upon IEnumerable<T>s the same things that you can use a foreach on and one thing you cannot do when you enumerate a collection with foreach is modifying that collection by removing items. Think of LINQ as pure functions, they never really modify stuff. They read from one enumerable and returns new things. It's all built on the Iterator pattern and very cool and smart stuff.

So the answer is you should not convert it to a LINQ expression. But I did anyway:

var returnString = new string (s.Reverse().SkipWhile(
                c =>
                    {
                        var skipped = false;
                        if (c == '&' && !skipped)
                        {
                            skipped = true;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }).Reverse().ToArray());

But don't use this, according to my primitive test this is about 3000 times slower then using TrimEnd

Zache
  • 1,023
  • 6
  • 14