Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a list of different characters written in a richtextbox.Now i want to pull one by one from the list using a loop and want to examine whether it is number or special character or word etc.
C#
var items = listView1.Items;
for (int i = 0; i < list.Count; i++)
{
    matchNumber(list[i].ToString(),""); //this loop isn't complete

}

C#
public void matchNumber(string number,string message)
{
    if (number == "0" || number == "1" || number == "2" || number == "3" || number == "4" || number == "5" || number == "6" || number == "7" || number == "8" || number == "9")
        message = "number";
}

public void matchSpecialCharacter(string specialCharacter, string message)
{
    if (specialCharacter == "," || specialCharacter == ";" || specialCharacter == "{" || specialCharacter == "}" || specialCharacter == "[" || specialCharacter == "]" || specialCharacter == "+" || specialCharacter == "-" || specialCharacter == "*" || specialCharacter == "/" || specialCharacter == "%")
        message = "special character";
}

matchNumber and matchSpecialCharacter are methods to filter each list[i] item.So i need to pass each list[i] to those methods as a parameter and return message as another parameter from those methods to print in loop.So how can i configure that loop to print it successfully for each list[i],whether it is number or other character.
Posted
Updated 2-Sep-12 6:25am
v2

Why split the test to two separate methods. What I mean is that could you simply have a single method, something like:
C#
public void DetermineType(char character, string message) {
   // test the type of the character
}

This way you wouldn't have to decide which method to call in the loop.

Another thing, if you want to test if a character is a number, you can use char.IsNumber[^]. For example:
C#
if(char.IsNumber(character)) {
   ...
}
 
Share this answer
 
v2
Comments
Manas Bhardwaj 2-Sep-12 13:28pm    
yup 5+
Wendelius 2-Sep-12 13:50pm    
Thanks :)
I would rather use a regular expression to search if it contains any number or any special character you mentioned.
 
Share this answer
 
Comments
Wendelius 2-Sep-12 13:50pm    
Good point, 5.
Manas Bhardwaj 2-Sep-12 13:52pm    
thx!
ridoy 4-Sep-12 12:04pm    
ya,i have done it using Regex.Match()

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