简体   繁体   English

查找字符串中第一个Char(Letter)的索引

[英]Find index of first Char(Letter) in string

I have a mental block and can't seem to figure this out, sure its pretty easy 0_o 我有一个思维障碍,似乎无法弄清楚,确保它非常容易0_o

I have the following string: "5555S1" 我有以下字符串:“ 5555S1”

String can contain any number of digits, followed by a Letter(AZ), followed by numbers again. 字符串可以包含任意数量的数字,后跟字母(AZ),再后跟数字。 How do I get the index of the Letter(S), so that I can substring so get everything following the Letter 如何获取字母的索引,以便我可以对字符串进行子字符串化,以便获取字母后面的所有内容

Ie: 5555S1 Should return S1 即5555S1应该返回S1

Cheers 干杯

You could also check if the integer representation of the character is >= 65 && <=90. 您还可以检查字符的整数表示形式是否为> = 65 && <= 90。

Simple Python: 简单的Python:

test = '5555Z187456764587368457638'

for i in range(0,len(test)):
    if test[i].isalpha():
            break

print test[i:]

Yields: Z187456764587368457638 产率:Z187456764587368457638

Given that you didn't say what language your using I'm going to pick the one I want to answer in - c# 鉴于您没有说出使用的语言,我将选择我想用的语言-C#

String.Index see http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx for more String.Index有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/system.string.indexof.aspx

for good measure here it is in java string.indexOf 在这里很好,它在java string.indexOf

One way could be to loop through the string untill you find a letter. 一种方法是遍历字符串,直到找到字母为止。

while(! isAlpha(s[i]) i++; or something should work. while(!isAlpha(s [i])i ++;或应该起作用。

This doesn't answer your question but it does solve your problem. 这不能回答您的问题,但可以解决您的问题。 (Although you can use it to work out the index) (尽管您可以使用它来计算索引)

Your problem is a good candidate for Regular Expressions (regex) 您的问题是正则表达式(regex)的很好的选择

Here is one I prepared earlier: 这是我之前准备的:

    String code = "1234A0987";

    //timeout optional but needed for security (so bad guys dont overload your server)
    TimeSpan timeout = TimeSpan.FromMilliseconds(150);


   //Magic here:
   //Pattern == (Block of 1 or more numbers)(block of 1 or more not numbers)(Block of 1 or more numbers)                      
    String regexPattern = @"^(?<firstNum>\d+)(?<notNumber>\D+)(?<SecondNum>\d+)?";
    Regex r = new Regex(regexPattern, RegexOptions.None, timeout);

    Match m = r.Match(code);
    if (m.Success)//We got a match!
    {     
        Console.WriteLine ("SecondNumber: {0}",r.Match(code).Result("${SecondNum}"));
        Console.WriteLine("All data (formatted): {0}",r.Match(code).Result("${firstNum}-${notNumber}-${SecondNum}")); 

        Console.WriteLine("Offset length (not that you need it now): {0}", r.Match(code).Result("${firstNum}").Length); 

    }

Output: 输出:

SecondNumber: 0987 
All data (formatted): 1234-A-0987 
Offset length (not that you need it now): 4

Further info on this example here . 有关此示例的更多信息,请参见此处

So there you go you can even work out what that index was. 这样一来,您甚至可以算出该索引是什么。

Regex cheat sheet 正则表达式备忘单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM