使用字符串生成器减少字符串长度 [英] Reduce String length using string builder

查看:119
本文介绍了使用字符串生成器减少字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是软件开发人员.现在我有一个问题.我需要使用字符串生成器来减小字符串的大小...我有一个类似"aaaaabbcbccc"的字符串,我想要一个这样的函数来逐个计算所有字符并给出输出类似"a5b3c4".
请尽快提供帮助....

Hello All,
I am software developer.Right now I have a problem.I need to reduce the size of a string using string builder...i have a string like "aaaaabbcbccc" i want a such function that count all the character one by one and gives the output like "a5b3c4".
Please help as soon as possible....

推荐答案

对不起.没看到您用VB.NET对其进行了标记
无论如何..这是我的C#示例. VB中的逻辑相同,应该易于转换.
Sorry. Didn''t see that you tagged it with VB.NET
Anyway.. Here''s my c# example. Same logic in VB, should be easy to convert..

var text = "aaaabbbbcccdddbbddaaccbbdd";
var values = new Dictionary<string, int>();

for (var i = 0; i < text.Length; i++)
{
    var character = text[i].ToString();
    if (values.ContainsKey(character))
    {
        // character exists. Increase value
        values[character] ++;
    }
    else
    {
        // character does not exists. Add character
        values.Add(character, 1);
    }
}

var sb = new StringBuilder();
foreach (KeyValuePair<string, int> pair in values)
{
    sb.Append(pair.Key + pair.Value);
}
Console.WriteLine(sb.ToString());
Console.Read();




如果要订购值,可以按以下方式循环:




If you want to order values you can loop this way:

foreach (KeyValuePair<string, int> pair in values.OrderBy(a=>a.Key))
{
    sb.Append(pair.Key + pair.Value);
}




试试这个,

Hi,

try this,

string str = "aaaaabbcbccc";
   string newstr = "";
        foreach (char ch in str.ToCharArray())
        {
            if (!str.Contains(ch))
                newstr += ch + fncheckcount(ch).ToString();
        }


private int fncheckcount(char c)
   {
       int count = 0;
       foreach (char ch in str.ToCharArray())
       {
           if (c == ch)
           {
               count++;
           }
       }
       return count;
   }



"newstr"将具有新的字符串值.

希望对您有所帮助.



the "newstr" will has the new string value.

hope it helps.


希望这就是您想要的

Hope this is what you want

static void Main(string[] args)
        {
            string text = "aAAAAabbbcccc";
            StringBuilder sb = new StringBuilder(text.Length);

            char prevChar = text[0];
            int prevCharCount = 1;

            foreach(char c in text)
            {
                if (c == prevChar) prevCharCount++;
                else
                {
                    sb.Append(prevChar);
                    sb.Append(prevCharCount);
                    
                    prevChar = c;
                    prevCharCount = 1;
                }
            }
            sb.Append(prevChar);
            sb.Append(prevCharCount);

            Console.WriteLine(sb.ToString());
        }



或直接使用



Or simply use

string result = "aAAbbbcccc";
Regex re = new Regex(@"(\w)\1*", RegexOptions.Compiled);
result = re.Replace(result, match => match.Value[0] + match.Length.ToString());



干杯...:)



Cheers... :)


这篇关于使用字符串生成器减少字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆