简体   繁体   English

正则表达式在字符串中查找第一个大写字母

[英]Regex to find first capital letter occurrence in a string

I want to find the index of first capital letter occurrence in a string. 我想在字符串中找到第一个大写字母出现的索引。

Eg - 例如 -

String x = "soHaM";

Index should return 2 for this string. 索引应该为此字符串返回2。 The regex should ignore all other capital letters after the first one is found. 正则表达式应该在找到第一个大写字母后忽略所有其他大写字母。 If there are no capital letters found then it should return 0. Please help. 如果没有找到大写字母,那么它应该返回0.请帮助。

I'm pretty sure all you need is the regex AZ \\p{Lu} : 我很确定你需要的是正则表达式 AZ \\p{Lu}

public static class Find
{
  // Apparently the regex below works for non-ASCII uppercase
  // characters (so, better than A-Z).
  static readonly Regex CapitalLetter = new Regex(@"\p{Lu}");

  public static int FirstCapitalLetter(string input)
  {
    Match match = CapitalLetter.Match(input);

    // I would go with -1 here, personally.
    return match.Success ? match.Index : 0;
  }
}

Did you try this? 你试过这个吗?

Just for fun, a LINQ solution: 只是为了好玩,一个LINQ解决方案:

string x = "soHaM";
var index = from ch in x.ToArray()
            where Char.IsUpper(ch)
            select x.IndexOf(ch);

This returns IEnumerable<Int32> . 这将返回IEnumerable<Int32> If you want the index of the first upper case character, simply call index.First() or retrieve only the first instance in the LINQ: 如果你想要第一个大写字符的索引,只需调用index.First()或只检索LINQ中的第一个实例:

string x = "soHaM";
var index = (from ch in x.ToArray()
            where Char.IsUpper(ch)
            select x.IndexOf(ch)).First();

EDIT 编辑

As suggested in the comments, here is another LINQ method (possibly more performant than my initial suggestion): 正如评论中所建议的,这是另一种LINQ方法(可能比我最初的建议更高效):

string x = "soHaM";
x.Select((c, index) => new { Char = c, Index = index }).First(c => Char.IsUpper(c.Char)).Index;

No need for Regex : 不需要正则Regex

int firstUpper = -1;
for(int i = 0; i < x.Length; i++)
{
    if(Char.IsUpper(x[i]))
    {
        firstUpper = i;
        break;
    }
}

http://msdn.microsoft.com/en-us/library/system.char.isupper.aspx http://msdn.microsoft.com/en-us/library/system.char.isupper.aspx

For the sake of completeness, here's my LINQ approach(although it's not the right tool here even if OP could use it): 为了完整起见,这是我的LINQ方法(虽然它不是正确的工具,即使OP可以使用它):

int firstUpperCharIndex = -1;
var upperChars = x.Select((c, index) => new { Char = c, Index = index })
                  .Where(c => Char.IsUpper(c.Char));
if(upperChars.Any())
    firstUpperCharIndex = upperChars.First().Index;

First your logic fails, if the method returns 0 in your case it would mean the first char in that list was in upperCase, so I would recomend that -1 meens not found, or throw a exception. 首先你的逻辑失败,如果方法在你的情况下返回0,那将意味着该列表中的第一个char在upperCase中,所以我建议找不到-1 meens,或者抛出异常。

Anyway just use regular expressions becasue you can is not always the best choise, plus they are pretty slow and hard to read in general, making yoru code much harder to work with. 无论如何只是使用正则表达式,因为你可能并不总是最好的选择,而且它们非常慢并且难以阅读,使得yoru代码更难以使用。

Anyway here is my contribution 无论如何,这是我的贡献

    public static int FindFirstUpper(string text)
    {
        for (int i = 0; i < text.Length; i++)
            if (Char.IsUpper(text[i]))
                return i;

        return -1;
    }

Using loop 使用循环

 int i = 0;
 for(i = 0; i < mystring.Length; i++)
 {
   if(Char.IsUpper(mystring, i))
    break;
 }

i is the value u should be looking at; 我是你应该看的价值;

Using Linq: 使用Linq:

using System.Linq;

string word = "soHaMH";
var capChars = word.Where(c => char.IsUpper(c)).Select(c => c);
char capChar = capChars.FirstOrDefault();
int index = word.IndexOf(capChar); 

Using C#: 使用C#:

using System.Text.RegularExpressions;

string word = "soHaMH";
Match match= Regex.Match(word, "[A-Z]");
index = word.IndexOf(match.ToString());

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

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