string s="abc_defg"; int ix = s.IndexOf('_'); s = ix != -1 ? s.Substring(ix + 1) : s;
using the ternary operator is completely useless here, it’s better to write:
s = s.Substring(ix + 1);
using the fact that Substring is probably optimized for case == 0 index
Is this what you want?
BUT someone suggested using LINQ guns, so
var temp = s.SkipWhile(p => p != '_').Skip(1); s = temp.Any() ? new string(temp.ToArray()) : s;
.NET 4.0 introduces a new string.Concat method.
s = temp.Any() ? string.Concat(temp) : s;
(note that in general, the LINQ method is slower and harder to read)
I will add ultrakill: Regular expressions !!! There is a school of thought that everything can be done using regular expressions OR jQuery! :-)
var rx = new Regex("(?:[^_]*_)?(.*)", RegexOptions.Singleline); var res = rx.Match(s).Groups[1].Value;
I will not even try to explain this beast to anyone, so do not ask. It's useless. (both regex and ask :-))
source share