c#中的字符串操作
本文关键字:操作 字符串 | 更新日期: 2023-09-27 17:49:42
我有一个这样的字符串,它是由一些反斜杠分隔的一些名称:
string mainString = @"Sean'John'Rob'fred";
我如何获得以上字符串格式的姓氏,在本例中为"fred",而我希望该名称是字符串中的姓氏(在所有反斜杠之后(?
谢谢。
你的意思是:
var list = mainString.Split('''');
return list[list.Length-1];
您可以使用LINQ来解决此问题:
string mainString = @"Sean'John''Rob'fred";
var fred = mainString
.Split("''".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.Last();
您也可以使用LastOrDefault()
来保护自己不受空字符串或不包含任何'
的字符串的影响。那么CCD_ 3将仅仅是CCD_。
首先,在字符串前面包含一个@,这样''就不会被视为转义序列:
string mainString = @"Sean'John'Rob'fred";
然后你可以这样得到你的姓氏:
string lastname = mainString.Substring( mainString.LastIndexOf('''')+1);
请注意,如果字符串不包含至少一个'',则会出现异常,因此在尝试获取子字符串之前,请进行检查以确认。
对于长输入,这应该比使用Split
更快,因为当您知道只需要最后一个值时,不必将字符串拆分为数组。
在这种情况下,使用Split
(正如大多数其他答案所建议的那样(是过分的,并且会毫无理由地分配临时数组。这个怎么样?
string lastString = mainString.Substring(mainString.LastIndexOf('''') + 1);
这可以在优化实现上不进行任何复制:
string mainString = @"Sean'John''Rob'fred";
var last = mainString.Reverse().TakeWhile(ch => '''' != ch).Reverse();
OP可能真的要求一个字符串,这太糟糕了,因为字符串是不可变的,这需要你构建一个新的字符串实例:
mainString = new string(last.ToArray());
并不是说我会这么做,但人们一直在寻找一种非侵入性的方式来做到这一点,所以…这里是
下面是mono 2.8.2 C#4.0(dmcs(编译器在优化+模式下为发出的IL
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
应该清楚哪种方法更优化:(
string[] strsplit=mainString.Split('''');
string laststring = strsplit[strsplit.length-1];
string mainString = @"Sean'John''Rob'fred";
var names = mainString.Split('''');
lastName = names[names.Length-1];
当您想按一个字符分割字符串时,您只需要执行Split()
,这将返回一个包含所有所需名称的数组。
string mainString = "Sean'John''Rob'fred";
string[] breakMe = mainString.Split('''');
// to get the 'fred' part:
breakMe [breakMe.length-1];
我不明白你关于另一个名字的问题。。。
string[] tokens = mainString.Split(new char[] { '''' }, StringSplitOptions.RemoveEmptyEntries);
string myString = tokens[tokens.Length - 1];
有几种方法。
- 在字符串中向后走。从结尾开始搜索,直到找到"''"为止
- 使用正则表达式,特别是使用回溯。检查单词/数字(如果允许的话(前面是否有"''"字符
string mainString = @"Sean'John''Rob'fred";
string[] fields = mainString.Split("''".ToCharArray());
if(fields.Length > 0) // found matches
Console.WriteLine(fields[fields.Length-1]); // fred
编辑:根据你的输入看起来如何,Öyvind Knobloch Bråthen的答案可能是一个更好的方法,因为它不搜索和分割整个字符串,因此速度要快得多。
这应该可以做到:-
string[] names = mainString.Split(new char[]{''''}, StringSplitOptions.RemoveEmptyEntries);
string result = names[names.Length - 1];