how to convert sb.toString() to char type?? who can help me..?

i tried already but still got error...because i wan to use in the foreach loop like
foreach(string a in sb.toString()) but this always give me the "Cannot convert Char type to String".What should i do..?

Since you have a character array, the first part of your foreach loop has to state Char, not string foreach(char a in sb.ToString()) There is only one string in a StringBuilder. foreach is for use with collections (IEnumerable) not single values.

foreach (char sort in sb.ToString().Trim())
                {          
                   if (!(sortList.ContainsKey(sort)))
                    {
                        sortList.Add(sort, 1);
                    }
                    else
                    {
                        int Count = (int)sortList[sort];
                        sortList[sort] = Count + 1;
                    }               
                } 

 DisplayResult();

   private void DisplayResult()
        {
            String ShowResult = "";
            
            IDictionaryEnumerator enumerator = sortList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                ShowResult += "{"+enumerator.Key.ToString()+"}:" + enumerator.Value.ToString();
            } 
         
            listBox.Items.Add(ShowResult.ToString());
        }

is it this coding got anything wrong..? my output always wrotng..?

try this...

foreach (char sort in sb.ToString().Trim().ToCharArray())
                {          
                   if (!(sortList.ContainsKey(sort)))
                    {
                        sortList.Add(sort, 1);
                    }
                    else
                    {
                        int Count = (int)sortList[sort];
                        sortList[sort] = Count + 1;
                    }               
                } 

 DisplayResult();

   private void DisplayResult()
        {
            String ShowResult = "";
            
            IDictionaryEnumerator enumerator = sortList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                ShowResult += "{"+enumerator.Key.ToString()+"}:" + enumerator.Value.ToString();
            } 
         
            listBox.Items.Add(ShowResult.ToString());
        }

still cannot...thanks for helping. Because now i want to find value for the sb.ToString().Trim().

for example:
Output for sb.ToString().Trim() is like this
1 2
1 3
1 4
2 3
2 4
3 4

This the key.
Now i wan to get their values.

foreach (char sort in sb.ToString().Trim())
                {          
                   if (!(sortList.ContainsKey(sort)))
                    {
                        sortList.Add(sort, 1);
                    }
                    else
                    {
                        int Count = (int)sortList[sort];
                        sortList[sort] = Count + 1;
                    }               
                }

The coding above is what i used to do to get the value for each key. My problem is the sb.ToString().Trim() cannot read in the foreach loop because it need array type. Now i need to use create one sortedList before i do in this foreach loop. I have no idea to do that. Who can help...?

Why do you put all the keys into a single string? Why not build a key and put it into a List<String> then build the next key, etc?

Why you dont use a Dictionary?
With:

Dictionary<int, List<int>> dic;
 //1st part is a key, 2nd part (generic list) are values

Edited 5 Years Ago by Mitja Bonca: n/a

This article has been dead for over six months. Start a new discussion instead.