Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 12-Nov-12 6:45am
ridoy51.8K
Edited 12-Nov-12 6:48am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Try this:
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.
  Permalink  
Comments
ridoy at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 

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");
 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanks for response.I solve it using following code..
 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 = "";
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 756
1 OriginalGriff 345
2 Magic Wonder 131
3 Kishore Pogaru 130
4 Fredrik Bornander 130
0 OriginalGriff 5,705
1 Sergey Alexandrovich Kryukov 4,763
2 CPallini 4,570
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100