C# 在字符串上找到特定字符时修剪字符串,是否可能?
假设我有这个字符串:C# 在字符串上找到特定字符时修剪字符串,是否可能?,c#,string,C#,String,假设我有这个字符串: String myString = "violet are blue|roses are red|this is a terrible poet"; 每当字符串找到字符“|”时,我都要修剪它。因此,当执行代码时,myString将是: myString = "roses are red|this is a terrible poet"; 最后 myString = "this is a terrible poet"; 有可能吗?您可以执行以下操作: string [
String myString = "violet are blue|roses are red|this is a terrible poet";
每当字符串找到字符“|”时,我都要修剪它。因此,当执行代码时,myString将是:
myString = "roses are red|this is a terrible poet";
最后
myString = "this is a terrible poet";
有可能吗?您可以执行以下操作:
string [] stringparts mystring.spilt('|');
stringsparts
可用于打印您想要的任何内容。您可以执行以下操作:
string [] stringparts mystring.spilt('|');
stringsparts
可用于打印您想要的任何内容。使用String.Split()
方法
//Single Line Solution
string newString1 = myString.Split('|').Last();
//Another way
string[] arr = myString.Split('|'); //Split the string by | into an array
string newString2 = arr[arr.Length-1]; //Get the last element of the splited array
Console.WriteLine(newString1);
使用String.Split()
方法
//Single Line Solution
string newString1 = myString.Split('|').Last();
//Another way
string[] arr = myString.Split('|'); //Split the string by | into an array
string newString2 = arr[arr.Length-1]; //Get the last element of the splited array
Console.WriteLine(newString1);
您可以使用
Substring
方法进行此操作,也许您希望将其用作扩展方法。代码包括修剪到第一个(左)和修剪到最后一个(右)
:
public static class StringExtensions
{
public static string TrimToFirst(this string s)
{
int index = s.IndexOf('|');
if(index >= 0)
{
s = s.Substring(index + 1);
}
return s;
}
public static string TrimToLast(this string s)
{
int index = s.LastIndexOf('|');
if(index >= 0)
{
s = s.Substring(index + 1);
}
return s;
}
}
// usage
string myString = "violet are blue|roses are red|this is a terrible poet";
Console.WriteLine(myString.TrimToLast());
// outpus: this is a terrible poet
您可以使用
Substring
方法进行此操作,也许您希望将其用作扩展方法。代码包括修剪到第一个(左)和修剪到最后一个(右)
:
public static class StringExtensions
{
public static string TrimToFirst(this string s)
{
int index = s.IndexOf('|');
if(index >= 0)
{
s = s.Substring(index + 1);
}
return s;
}
public static string TrimToLast(this string s)
{
int index = s.LastIndexOf('|');
if(index >= 0)
{
s = s.Substring(index + 1);
}
return s;
}
}
// usage
string myString = "violet are blue|roses are red|this is a terrible poet";
Console.WriteLine(myString.TrimToLast());
// outpus: this is a terrible poet
用法
String myString = "violet are blue|roses are red|this is a terrible poet";
myString = trimString(myString);
用法
String myString = "violet are blue|roses are red|this is a terrible poet";
myString = trimString(myString);
下面是实现这一点的100000000种方法之一
string myString = "violet are blue|roses are red|this is a terrible poet";
string result = myString.Contains('|') ? myString.Substring(myString.IndexOf('|')).TrimStart('|') : myString;
下面是实现这一点的100000000种方法之一
string myString = "violet are blue|roses are red|this is a terrible poet";
string result = myString.Contains('|') ? myString.Substring(myString.IndexOf('|')).TrimStart('|') : myString;
实现它的一个简单方法:-
myString = myString.Substring(myString.LastIndexOf("|") + 1);
实现它的一个简单方法:-
myString = myString.Substring(myString.LastIndexOf("|") + 1);
当
|
不在字符串中时,它会将字符串修剪为“|玫瑰是红色的|这是一个糟糕的诗人”,并且当“|”保留在字符串的第一个字母上时,下一个“|”将不再修剪。没有效率,不需要向下投票,只需要纠正。人们都很有趣。就像投否决票一样。只需索引+1
:)很抱歉,这是因为我以为你没有检查索引==-1
,但现在我明白了,它非常聪明,转换为upvote。为了澄清我没有进行downvote,只是等待更好的答案或编辑,这实际上是有效的,你已经提供了。因此,向上投票并接受回答:)当
不在字符串中时,它会将字符串修剪为“|玫瑰是红色的|这是一个糟糕的诗人”,而“|”保留在字符串的第一个字母上,下一个“|”将不会再次修剪。没有效率,不需要向下投票,只需要纠正。人们都很有趣。就像投否决票一样。只需索引+1
:)很抱歉,这是因为我以为你没有检查索引==-1
,但现在我明白了,它非常聪明,转换为upvote。为了澄清我没有进行downvote,只是等待更好的答案或编辑,这实际上是有效的,你已经提供了。所以投票并回答:)我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。