12,546,064 members (56,456 online)
Rate this:
Please Sign up or sign in to vote.
See more:
```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
ridoy58.7K
Updated 12-Nov-12 6:48am
v2

## 4 solutions

Rate this:
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 = "";
}```
Rate this:
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.
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.
Rate this:
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");

}```
Rate this:
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

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

Top Experts
Last 24hrsThis month
 OriginalGriff 240 CPallini 235 Karthik Bangalore 110 Mika Wendelius 109 Maciej Los 100
 OriginalGriff 3,627 John Simmons / outlaw programmer 1,627 ppolymorphe 1,441 Suvendu Shekhar Giri 1,378 Karthik Bangalore 1,160

Advertise | Privacy | Mobile
Web02 | 2.8.161018.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2016
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