如何让这2种方法更高效

发布于 2024-12-05 01:04:26 字数 1459 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玉环 2024-12-12 01:04:26

您可以使用以下扩展方法根据大写字母拆分字符串:

public static string Wordify(this string camelCaseWord)
    {
        /* CamelCaseWord will become Camel Case Word,  
          if the word is all upper, just return it*/

        if (!Regex.IsMatch(camelCaseWord, "[a-z]"))
            return camelCaseWord;

        return string.Join(" ", Regex.Split(camelCaseWord, @"(?<!^)(?=[A-Z])"));
    }

要拆分字符串数组中的字符串,您可以使用以下方法:

public static string[] SplitOnVal(this string text,string value)
    {
        return text.Split(new[] { value }, StringSplitOptions.None);
    }

如果我们考虑您的示例,代码将如下所示:

string strTest = "AdnanRazaBhatti";
var capitalCase = strTest.Wordify(); //Adnan Raza Bhatti
var brokenResults = capitalCase.SplitOnVal(" ");  //seperate by a blank value in an array

you can use the following extension methods to split your string based on Capital letters:

public static string Wordify(this string camelCaseWord)
    {
        /* CamelCaseWord will become Camel Case Word,  
          if the word is all upper, just return it*/

        if (!Regex.IsMatch(camelCaseWord, "[a-z]"))
            return camelCaseWord;

        return string.Join(" ", Regex.Split(camelCaseWord, @"(?<!^)(?=[A-Z])"));
    }

To split a string in a string array, you can use this:

public static string[] SplitOnVal(this string text,string value)
    {
        return text.Split(new[] { value }, StringSplitOptions.None);
    }

If we take your example for consideration, the code will be as follows:

string strTest = "AdnanRazaBhatti";
var capitalCase = strTest.Wordify(); //Adnan Raza Bhatti
var brokenResults = capitalCase.SplitOnVal(" ");  //seperate by a blank value in an array
甜嗑 2024-12-12 01:04:26

检查此代码

public static string SeperateCamelCase(this string value) 
{
  return Regex.Replace(value, "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1");
} 

希望这个答案对您有帮助。如果您找到解决方案,请标记我的答案并指出。

Check this code

public static string SeperateCamelCase(this string value) 
{
  return Regex.Replace(value, "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1");
} 

Hope this answer helps you. If you find solution kindly mark my answer and point it up.

疾风者 2024-12-12 01:04:26

在我看来,正则表达式是最好的选择。

我认为 [AZ][az]+ 可能是一个不错的起点。

Looks to me like regular expressions is the way to go.

I think [A-Z][a-z]+ might be a good one to start with.

旧故 2024-12-12 01:04:26

更新版本。字符串生成器用于减少内存利用率。

string SplitCapital(string str)
{
    //Search all capital letters and store indexes
    var indexes = str
        .Select((c, i) => new { c = c, i = i }) // Select information about char and position
        .Where(c => Char.IsUpper(c.c)) // Get only capital chars
        .Select(cl => cl.i); // Get indexes of capital chars

    // If no indexes found or if indicies count equal to the source string length then return source string
    if (!indexes.Any() || indexes.Count() == str.Length)
    {
        return str;
    }

    // Create string builder from the source string
    var sb = new StringBuilder(str);
    // Reverse indexes and remove 0 if necessary
    foreach (var index in indexes.Reverse().Where(i => i != 0))
    {
        // Insert spaces before capital letter
        sb.Insert(index, ' ');
    }

    return sb.ToString();
}

string SplitCapital(string str, out string[] parts)
{
    var splitted = SplitCapital(str);
    parts = splitted.Split(new[] { ' ' }, StringSplitOptions.None);
    return splitted;
}

Updated version. String builder was used to reduce memory utilization.

string SplitCapital(string str)
{
    //Search all capital letters and store indexes
    var indexes = str
        .Select((c, i) => new { c = c, i = i }) // Select information about char and position
        .Where(c => Char.IsUpper(c.c)) // Get only capital chars
        .Select(cl => cl.i); // Get indexes of capital chars

    // If no indexes found or if indicies count equal to the source string length then return source string
    if (!indexes.Any() || indexes.Count() == str.Length)
    {
        return str;
    }

    // Create string builder from the source string
    var sb = new StringBuilder(str);
    // Reverse indexes and remove 0 if necessary
    foreach (var index in indexes.Reverse().Where(i => i != 0))
    {
        // Insert spaces before capital letter
        sb.Insert(index, ' ');
    }

    return sb.ToString();
}

string SplitCapital(string str, out string[] parts)
{
    var splitted = SplitCapital(str);
    parts = splitted.Split(new[] { ' ' }, StringSplitOptions.None);
    return splitted;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文