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

Recommended Answers

All 11 Replies

You can convert it to a character array by appending .ToCharArray()

like this sb.ToCharArray() or (sb.ToStirng()).ToCharArray()??

sb.ToString().ToCharArray()

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

the List <string> is the SortedList??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.