在字符串C#上分隔大写和小写

时间:2017-03-20 06:18:24

标签: c#

有人可以建议一种方法,其中我可以分隔字符串中的大写字符和小写字符

input  : "heLLoWorLd"
output : "heoordLLWL"

7 个答案:

答案 0 :(得分:1)

我准备了一个程序让你做同样的事情:

using System;
using System.Collections.Generic;
using System.IO;

    class Program
    {
        static void Main()
        {
            // Input string.
            string mixedCase = "heLLoWorLd";

            // Call ToLower instance method, which returns a new copy.
            string lower = "";
            string uper = "";
            for (int i = 0; i < mixedCase.Length; i++)
            {
                if (char.IsLower(mixedCase[i]))
                    lower = lower + mixedCase[i];
                else
                    uper = uper + mixedCase[i];
            }

            // Display results.
            Console.WriteLine("{0}{1}",
                lower,
                uper);
        }
    }

<强>输出:

heoordLLWL

这段代码肯定对你有所帮助。谢谢!

答案 1 :(得分:1)

您可以使用以下几种扩展方法:

string strInput="heLLoWorLd";
string outputStr =String.Join("",strInput.GroupBy(x=>Char.IsLower(x))
                                         .SelectMany(y=>y.ToList()));

您可以尝试一个工作示例here

答案 2 :(得分:1)

为什么不只是一个简单的OrderBy? Imo GroupBySelectMany有点用大锤敲打坚果

string input =    "heLLoWorLd";
string output = string.Concat(input.OrderBy(char.IsUpper)); // heoordLLWL

答案 3 :(得分:0)

试试这个:

 string input = "heLLoWorLd";
 string output = string.Empty;
 output = String.Concat(input.Where(c => Char.IsLower(c))) + String.Concat(input.Where(c => Char.IsUpper(c))) ;

答案 4 :(得分:0)

string input = "heLLoWorLd";
StringBuilder builder = new StringBuilder();
StringBuilder upp = new StringBuilder();
StringBuilder low = new StringBuilder();
foreach (char c in input)
{
    if (Char.IsLower(c))
    {
        low.Append(c);
    }
    else
    {
        if (Char.IsUpper(c))
        {
            upp.Append(c);
        }
    }
}
string output = low.ToString() + upp.ToString();

答案 5 :(得分:0)

另一种方式,

string str = "WeLcoMe";
string _upper = string.Empty, _lower = string.Empty;
foreach (var s in str)
{
    if (char.IsUpper(s))
        _upper += s;
    else
        _lower += s; 
}
str = _upper + _lower;

输出

WLMecoe

答案 6 :(得分:0)

var input = "heLLoWorLd";
var output = string.Concat(input.GroupBy(char.IsLower).SelectMany(c => c.ToList()));