[英]Check for specific letter in string
I am trying to check if the first letter in a string is l, the l can be upper or lower case, if it is an l I want to trim it from the string and I am currently using this code to do it 我正在尝试检查字符串中的第一个字母是l,l可以是大写还是小写,如果是l,我想从字符串中修剪它,而我目前正在使用此代码来做到这一点
String firstLetter = result.Text[0].ToString();
if (result.Text.Length == 18)
{
if (firstLetter.Equals("l".ToString()) || firstLetter.Equals("L".ToString()))
{
result.Text.Remove(0, 1);
}
if (firstLetter == "l" || firstLetter == "L" || firstLetter == "1")
{
result.Text.Remove(0, 1);
}
if (result.Text.StartsWith("l".ToString()) || result.Text.ToUpper().StartsWith("L".ToString()))
{
result.Text.Remove(0, 1);
}
}
none of these if statements have worked, they are completely skipped over, why arent they working? 这些if语句都不起作用,它们被完全跳过,为什么不起作用?
All you need to do is: 您需要做的只是:
result = result.TrimStart({'L', 'l'});
This will take "Llama" and make it "ama". 这将采用“骆驼”并将其设为“ ama”。 If you are trying to take "Llama" and get "lama", use this code:
如果您尝试使用“骆驼”并获得“骆驼”,请使用以下代码:
result = result.ToUpper().StartsWith("L") ? result.Remove(0,1) : result;
If your .Text[] isn't exaclty 18 then it won't go in there. 如果您的.Text []不正确,那么它将不会进入18。
Once you get the text out of whatever your result is just do this. 一旦从结果中得到文本,就执行此操作。
if (firstLetter.Substring(0, 1).ToUpper() == "L")
{
// equals l so lets remove it
firstLetter = firstLetter.Remove(0, 1);
}
You should try this: 您应该尝试这样:
bool isFirstL = firstLetter.Substring(0, 1).ToUpper().Equals("L");
And you can use this expression in the way you want, not to assign value for bool. 而且,您可以按需要使用此表达式,而不必为bool赋值。
BTW. 顺便说一句。 you have line
if (result.Text.Length == 18)
does your string really contains 18 characters? 您是否有一行
if (result.Text.Length == 18)
您的字符串是否真的包含18个字符?
我必须做:
result = result.TrimStart(new char[] {'L', 'l'});
In C# the string type is designed to be immutable, meaning that you cannot change a string instance. 在C#中,字符串类型被设计为不可变的,这意味着您不能更改字符串实例。 This is from the documentation of the Remove method , emphasis mine:
这来自Remove方法的文档 ,重点是:
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
返回一个新字符串 ,其中已删除当前实例中从指定位置开始的指定数量的字符。
So to actually see the result of the call to Remove, you will have to assign it to something, for example: 因此,要实际查看对Remove的调用结果,您必须将其分配给某些对象,例如:
result.Text = result.Text.Remove(0, 1);
To see if the string starts with an L, the cleanest way would be to use StartsWith . 要查看字符串是否以L开头,最干净的方法是使用StartsWith 。 The entire code would like this:
整个代码如下:
if (result.Text.Length == 18 &&
result.Text.StartsWith("L", StringComparison.OrdinalIgnoreCase))
{
result.Text = result.Text.Remove(0, 1);
}
Just use string.IndexOf
, which takes IgnoreCase
parameter, like: 只需使用
string.IndexOf
IgnoreCase
参数的string.IndexOf
,例如:
string firstLetter = "LlSomething";
if (firstLetter.IndexOf("l", StringComparison.InvariantCultureIgnoreCase) == 0
&& firstLetter.Length > 0)
{
firstLetter = firstLetter.Substring(1);
}
and you will get lSomething
as the output. 您将获得
lSomething
作为输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.