String Manipulation in C #

I have a line like this, these are some names separated by some backslashes:

string mainString = @"Sean\John\Rob\fred";

How can I get the last name in the format of the line above, in this case "fred", while I want the name to be the last in the line (after all backslashes)?

Thank.

+5
source share
12 answers

Do you mean:

var list = mainString.Split('\\');
return list[list.Length-1];
+13
source

You can use LINQ to solve this problem:

string mainString = @"Sean\John\\Rob\fred";
var fred = mainString
   .Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
   .Last();

You can also use LastOrDefault()to protect yourself from an empty string or a string that does not contain \. Then it fredwill be simple null.

+9
source

, @in, \ escape-:

string mainString = @"Sean\John\Rob\fred";

:

string lastname = mainString.Substring( mainString.LastIndexOf('\\')+1);

, , \, , , .

, Split, , , .

+8

Split - - . ?

string lastString = mainString.Substring(mainString.LastIndexOf('\\') + 1);
+3

:

        string mainString = @"Sean\John\\Rob\fred";
        var last = mainString.Reverse().TakeWhile(ch => '\\' != ch).Reverse();

, OP , , , , :

        mainString = new string(last.ToArray());

, , , ...


, IL, mono 2.8.2 # 4.0 (dmcs) - +

mainString.Substring(mainString.LastIndexOf('\\') + 1)

    .locals init (string  V_0)
    IL_0000:  ldstr "Sean\\John\\\\Rob\\fred"
    IL_0005:  stloc.0
    IL_0006:  ldloc.0
    IL_0007:  ldloc.0
    IL_0008:  ldc.i4.s 0x5c
    IL_000a:  callvirt instance int32 string::LastIndexOf(char)
    IL_000f:  ldc.i4.1
    IL_0010:  add
    IL_0011:  callvirt instance string string::Substring(int32)
    IL_0016:  stloc.0

mainString.Reverse().TakeWhile(ch => '\\' != ch).Reverse()

mainString = new string(last.ToArray());

   .locals init (string  V_0, class [mscorlib]System.Collections.Generic.IEnumerable`1<char>  V_1)
   IL_0000:  ldstr "Sean\\John\\\\Rob\\fred"
   IL_0005:  stloc.0
   IL_0006:  ldloc.0
   IL_0007:  call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> class [System.Core]System.Linq.Enumerable::Reverse<char> (class [mscorlib]System...
   IL_000c:  ldsfld class [mscorlib]System.Func`2<char,bool> qqq.MainClass::'<>f__am$cache0'
   IL_0011:  brtrue.s IL_0024

   IL_0013:  ldnull
   IL_0014:  ldftn bool class qqq.MainClass::'<Main>m__0'(char)
   IL_001a:  newobj instance void class [mscorlib]System.Func`2<char, bool>::'.ctor'(object, native int)
   IL_001f:  stsfld class [mscorlib]System.Func`2<char,bool> qqq.MainClass::'<>f__am$cache0'
   IL_0024:  ldsfld class [mscorlib]System.Func`2<char,bool> qqq.MainClass::'<>f__am$cache0'
   IL_0029:  call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> class [System.Core]System.Linq.Enumerable::TakeWhile<char> (class [mscorlib]Syst...
   IL_002e:  call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> class [System.Core]System.Linq.Enumerable::Reverse<char> (class [mscorlib]System...
   IL_0033:  stloc.1
   IL_0034:  ldloc.1
   IL_0035:  call !!0[] class [System.Core]System.Linq.Enumerable::ToArray<char> (class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>)
   IL_003a:  newobj instance void string::'.ctor'(char[])
   IL_003f:  stloc.0

   //
   //
   // With the Lambda expression (ch => '\\' != ch) compiled to:
   // 
   // method line 3
   .method private static hidebysig
          default bool '<Main>m__0' (char ch)  cil managed
   {
       .custom instance void class [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::'.ctor'() =  (01 00 00 00 ) // ....

       // Method begins at RVA 0x214c
       // Code size 9 (0x9)
       .maxstack 8
       IL_0000:  ldc.i4.s 0x5c  // '\\' character
       IL_0002:  ldarg.0
       IL_0003:  ceq
       IL_0005:  ldc.i4.0
       IL_0006:  ceq
       IL_0008:  ret
   } // end of method MainClass::<Main>m__0

, :)

+2
string[] strsplit=mainString.Split('\\');
string laststring = strsplit[strsplit.length-1];
+1

, , , Split(), , .

string mainString = "Sean\John\\Rob\fred";

string[] breakMe = mainString.Split('\\');

// to get the 'fred' part:
breakMe [breakMe.length-1];

...

+1
 string mainString = @"Sean\John\\Rob\fred";
    var names = mainString.Split('\\');
    lastName = names[names.Length-1];
+1
string[] tokens = mainString.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
string myString = tokens[tokens.Length - 1];
0

.

  • . , "\".
  • , , . / ( ), "\".
0
string mainString = @"Sean\John\\Rob\fred";
string[] fields = mainString.Split("\\".ToCharArray());
if(fields.Length > 0) // found matches
    Console.WriteLine(fields[fields.Length-1]); // fred

Edit: Depending on what your input looks like, Øyvind Knobloch-Bråthen's answer might be a better approach, as it does not search and break the entire string, and therefore, much faster.

0
source

This should do the trick: -

 string[] names = mainString.Split(new char[]{'\\'}, StringSplitOptions.RemoveEmptyEntries);
 string result = names[names.Length - 1];
0
source

All Articles