Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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....
Posted
Updated 18-Jul-12 0:17am
v2

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..

C#
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:
C#
foreach (KeyValuePair<string, int> pair in values.OrderBy(a=>a.Key))
{
    sb.Append(pair.Key + pair.Value);
}
 
Share this answer
 
v3
Comments
Kenneth Haugland 18-Jul-12 6:29am    
clever.... but its C#
StianSandberg 18-Jul-12 6:31am    
yeah.. just saw it. I don't do VB, but it's easy to convert..
shivammpi123 18-Jul-12 7:03am    
thank u very much it works....HAPPY CODING
StianSandberg 18-Jul-12 6:32am    
I think using a Dictionary is the easiest way to do this. Maybe you can translate my code to VB?
Kenneth Haugland 18-Jul-12 6:36am    
Simple code converter:
http://www.developerfusion.com/tools/convert/csharp-to-vb/

Complete program converter:
http://converter.telerik.com/

Both useful....
Hi,

try this,

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

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


the "newstr" will has the new string value.

hope it helps.
 
Share this answer
 
Hope this is what you want

C#
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
C#
string result = "aAAbbbcccc";
Regex re = new Regex(@"(\w)\1*", RegexOptions.Compiled);
result = re.Replace(result, match => match.Value[0] + match.Length.ToString());


Cheers... :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900