C#使用多个分隔符拆分字符串

huangapple go评论76阅读模式
英文:

C# split string with multiple separators

问题

string one = @"first%second";
string two = @"first%second%third";
string three = @"first%second%third%fourth";

string oneresult = @"second";
string tworesult = @"second%third";
string threresult = @"second%third%fourth";
英文:

Strings:

string one = @"first%second";
string two = @"first%second%third";
string three = @"first%second%third%fourth";

I need to be able to separate everything that comes after the first '%' delimiter.
I'd normally use split function:

string partofstring = one.Split('%').Last();

or

string partofstring = one.Split('%').[1];

However I need to be able to get:

string oneresult = @"second";
string tworesult = @"second%third";
string threresult = @"second%third%fourth";

答案1

得分: 4

string.Split有一个重载,允许你指定要返回的拆分数。将2指定为拆分数,你将得到一个数组,其中索引为0的元素可以丢弃,索引为1的元素正是所需的输出。

string three = @"first%second%third%fourth";
var result = three.Split(new char[] { '%' }, 2);
Console.WriteLine(result[1]); // ==> second%third%fourth
英文:

string.Split has an overload that allows you to specify the number of splits that you want to get back. Specifying 2 as the number of splits you will get an array with the element at index 0 that you can discard and the element at index 1 that is exactly the output requested

string three = @"first%second%third%fourth";
var result = three.Split(new char[] {'%'}, 2);
Console.WriteLine(result[1]); // ==> second%third%fourth

答案2

得分: 2

[string.SubString](https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8) 返回从指定位置到字符串末尾的子字符串。

[string.IndexOf](https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.8) 返回字符串中指定字符的第一个索引。
英文:

Try this

string partofstring = one.SubString(one.IndexOf('%'));

String.SubString returns a string starting from the specified position to the end of the string.

String.IndexOf returns the first index of the specified character in the string.

答案3

得分: 0

使用不同的 string.Split 重载,允许您指定最大项目数:

var newString = myString.Split('%', 2)[1];

请注意,如果您使用 .NET Framework,您需要使用不同的重载:

var newString = three.Split(new[] { '%' }, 2)[1];

或者,您可以自行计算,使用 IndexOfSubstring

var newString = myString.Substring(myString.IndexOf('%') + 1);
英文:

Either use a different overload of string.Split that allows you to specify the maximum number of items:

var newString = myString.Split('%', 2)[1];

Note if you're using .NET Framework, you will need to use a different overload:

var newString = three.Split(new[] { '%'}, 2)[1];

Or, calculate it yourself using IndexOf and Substring:

var newString = myString.Substring(myString.IndexOf('%') + 1);

答案4

得分: 0

我改进了@Preciousbetine提供的代码片段:

var partofstring = two.IndexOf('%') < 0 ? string.Empty : two.Substring(two.IndexOf('%') + 1);

这将检查字符串是否不包含关键字%。
英文:

I've improved the code snippet given by @Preciousbetine

var partofstring = two.IndexOf(&#39;%&#39;) &lt; 0 ? string.Empty : two.Substring(two.IndexOf(&#39;%&#39;) + 1);

This will check if the string doesn't contain the key %.

huangapple
  • 本文由 发表于 2020年1月6日 17:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定