Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping StringBuilder strings

Posted on 2008-10-28
5
Medium Priority
?
1,646 Views
Last Modified: 2012-05-05
Hi,
    I have a C# StringBuilder with the below strings:

2.1.1 ICSS
2.1.1 ICSS
2.1.1 ICSS
2.1.4 User Interface
2.1.4 User Interface  
2.1.5 Alarm Management  
2.1.5 Alarm Management  
2.1.5 Alarm Management  
2.1.5 Alarm Management

How can I loop through these strings & count the total of each type. e.g. The results would be:
2.1.1. ICSS = 3
2.1.4 User Interface = 2
2.1.5 Alarm management = 4

Thanks
Paul.
0
Comment
Question by:pr_wainwright
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22823431
How are they delimited?

Regardless, use a HashTable type structure and add a counter variable to it using the strings as the key.  If the key already exists then increment the value already stored there.

What version C# are you using?
0
 
LVL 1

Author Comment

by:pr_wainwright
ID: 22823520
Idle_Mind,
                I am using Visual Web Developer 2005. I have never used hash tables before. Any help on these appreciated.

Thanks
Paul.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22823695
How are the strings delimited in the StringBuilder?....are they separated by comma, tab, something else?....
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 22824367
I don't do web development...but hopefully this should give a good idea of how it can be done:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private StringBuilder sb = new StringBuilder();
 
        private void Form1_Load(object sender, EventArgs e)
        {
            sb.Append("2.1.1 ICSS" + ",");
            sb.Append("2.1.1 ICSS" + ",");
            sb.Append("2.1.1 ICSS" + ",");
            sb.Append("2.1.4 User Interface" + ",");
            sb.Append("2.1.4 User Interface" + ",");
            sb.Append("2.1.5 Alarm Management" + ",");
            sb.Append("2.1.5 Alarm Management" + ",");
            sb.Append("2.1.5 Alarm Management" + ",");
            sb.Append("2.1.5 Alarm Management");
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> counts = new Dictionary<string, int>();
            foreach (String value in sb.ToString().Split(",".ToCharArray()))
            {
                if (!counts.ContainsKey(value))
                {
                    counts.Add(value, 1);
                }
                else
                {
                    counts[value] = counts[value] + 1;
                }
            }
 
            foreach (String key in counts.Keys)
            {
                MessageBox.Show(key + " = " + counts[key].ToString());
            }
        }
 
    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:pr_wainwright
ID: 31510792
Exactly what I needed. Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question