Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
char[] charArray = new char[1000];
charArray = richTextBox1.Text.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
    int c = Convert.ToInt32(charArray[i]);
    if(c>=48 && c<=57)
     listView1.Items.Add(charArray[i].ToString() + "-->number");

     else if (Char.IsLetter(charArray[i]))
      {
          int c1 = Convert.ToInt32(charArray[i]);
         if (c1 >= 65 && c1 <= 90 || c1 >= 97 && c1 <= 122)
         listView1.Items.Add(charArray[i].ToString() + "-->alphabet");
                                }
}

Got stack in a small problem.Try to find numbers and words from this loop.But what i do is..it prints as letter or digit for each character.But i want if i write 123.45 or write WORD in richtextbox it will show output as 123.45 is a number and WORD is a word in listview.but my loop prints as like as...
1-->number
2-->number
.
.
W-->alphabet
O-->alphabet
.
.
So how can i combine these characters as number or word?Is my checking criteria is wrong?
Posted
Updated 12-Nov-12 6:48am
v2

Thanks for response.I solve it using following code..
C#
 while(i<chararray.length)>
             {  
                while (Char.IsDigit(charArray[i]) || Char.IsLetter(charArray[i]))
                 {
                      word = word + charArray[i];
                      i++;
                 }
                         
                      double num;

                      if (double.TryParse(word, out num))
                      {
                           if (charArray[i].ToString() == ".")
                           {
                                word += charArray[i];
                                i++;
                                while (Char.IsDigit(charArray[i]))
                                {
                                    word += charArray[i];
                                    i++;
                                }
                            }
                            listView1.Items.Add(word +"-->number");
                       }
                       else
                       {
                           listView1.Items.Add(word +"--->word");
                       }
                       word = "";
}
 
Share this answer
 
Hi,

Try this:
C#
string[] parts = richTextBox1.Text.Split(' '); // split the text
foreach (string part in parts)
{
      double d = 0;
      if (double.TryParse(part,d)) // try to parse the current part to a number
      {
            listView1.Items.Add(part + "-->number"); // if you can parse, add part + "-->number" to the ListView
      }
      else
      {
            listView1.Items.Add(part + "-->word"); // if you can't parse, the current part is a word, add part + "-->word" to the ListView
      }          
}


Hope this helps.
 
Share this answer
 
Comments
ridoy 12-Nov-12 13:00pm    
Thanks for your reply,but sorry i need to escape whitspace for what i can't check those characters by detecting whitespace.
You will need to store the chars in a variable then print them when you change to the other type. Below is an example. This example doesn't check for punctuation or white space though you could easily add that.


C#
private void ParseThis()
        {
            char[] charArray = new char[1000];
            charArray = richTextBox1.Text.ToCharArray();
            string number = string.Empty;
            string word = string.Empty;

            for (int i = 0; i < charArray.Length; i++)
            {
                int c = Convert.ToInt32(charArray[i]);
                if (c >= 48 && c <= 57)
                {
                    if (number.Length == 0)
                    {
                        if (word.Length > 0)
                            listView1.Items.Add(word + "-->alphabet");
                        word = string.Empty;
                    }
                    number += charArray[i].ToString();
                }

                else if (Char.IsLetter(charArray[i]))
                {
                    int c1 = Convert.ToInt32(charArray[i]);
                    if (c1 >= 65 && c1 <= 90 || c1 >= 97 && c1 <= 122)
                    {
                        if (word.Length == 0)
                        {
                            if (number.Length > 0)
                                listView1.Items.Add(number + "-->number");
                            number = string.Empty;
                        }

                        word += charArray[i].ToString();
                    }
                }
            }


            if (word.Length > 0)
                listView1.Items.Add(word + "-->alphabet");
            if (number.Length > 0)
                listView1.Items.Add(number + "-->number");

        }
 
Share this answer
 
If I understood correctly, if input is 123.45, it should say "NUMBER" else it should say "WORD".
If this is true, why dont you try double.TryParse ? and Int.TryParse?
It will be something like

C#
double value;
bool isNumber = double.TryParse(richTextBox1.Text,out value);
if(isNumber)
{
  listView1.Items.Add("NUMBER");
}
else
{
  listView1.Items.Add("WORD");
}


Hope that helps
Milind
 
Share this answer
 

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