Null Pointer
Null Pointer

Reputation: 9309

Which is the best way to skip characters before underscore ( _ ) in a string using c#

Which is the best way to skip characters before underscore _ in a string using c#?

eg:case 1 String contain _

string s="abc_defg" 

I want to get defg to another string

case 2

some times string do not contain _ . at that time i need to get the all string

eg.

s="defg"

In both case i want get "defg" . Filtering only applied if there is an underscore in the string. How can i do that

Upvotes: 4

Views: 3865

Answers (5)

iJade
iJade

Reputation: 23811

I would suggest simply use a string[] arr and split function for this purpose.

string[] arr=s.split('_');
string out=arr[arr.Length-1];

Upvotes: 0

xanatos
xanatos

Reputation: 111920

string s="abc_defg";
int ix = s.IndexOf('_');
s = ix != -1 ? s.Substring(ix + 1) : s;

using the ternary operator here is quite useless, better to write:

s = s.Substring(ix + 1);

directly, using the fact that Substring probably is optimized for the case index == 0

Is this what you want?

BUT someone has suggested using LINQ cannons, so

var temp = s.SkipWhile(p => p != '_').Skip(1);
s = temp.Any() ? new string(temp.ToArray()) : s;

In .NET 4.0 there is a new string.Concat method.

s = temp.Any() ? string.Concat(temp) : s;

(note that in general the LINQ way is slower and more complex to read)

I'll add the ultrakill: the Regular Expressions!!! There is a school of thought that anything can be done with Regular Expressions OR jQuery! :-)

var rx = new Regex("(?:[^_]*_)?(.*)", RegexOptions.Singleline);
var res = rx.Match(s).Groups[1].Value;

I won't even try to explain this beast to anyone, so don't ask. It's useless. (both the Regex and to ask :-) )

Upvotes: 6

sll
sll

Reputation: 62544

You can leverage LINQ SkipWhile():

textValue.SkipWhile(c => c != separator)
         .Skip(1)

Final solution for both cases:

string textValue = "abc_defg";
char separator = '_';
string result = textValue.IndexOf(separator) >= 0 
                ? new String(textValue.SkipWhile(c => c != separator)
                                      .Skip(1)
                                      .ToArray())
                : textValue;

EDIT:

I'm right with xanatos comment, but anyway sometimes it is pretty interesting to find out different solutions :)

Upvotes: 2

Jonas Elfström
Jonas Elfström

Reputation: 31448

I do not know how to define the best way but this works as long as there is only one underscore.

string s1 = "abc_defg";
string s2 = "defg";

Console.WriteLine( s1.Split('_').Last() );
Console.WriteLine( s2.Split('_').Last() );

If you need more flexibility you can take a look at regular expressions.

Regex.Replace(s1, "^.*_", "")

Upvotes: 6

Jamiec
Jamiec

Reputation: 136164

simple function perhaps:

public string TakeStringAfterUnderscoreOrWholeString(string  input)
{
   var underscorePos = input.IndexOf("_");
   return underscorePos  == -1 ? input : input.Substring(underscorePos+1);
}

Live test: http://rextester.com/rundotnet?code=AVQO61388

Upvotes: 2

Related Questions