从字符中获取数字和单词 [英] Get numbers and words from characters

查看:64
本文介绍了从字符中获取数字和单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
                                }
}


在一个小问题中出现了堆栈.尝试从此循环中查找数字和单词.但是我所做的是..每个字符打印为字母或数字.但是我想如果我在Richtextbox中写123.45或WORD,它将显示输出因为123.45是一个数字,而WORD是listview中的一个单词.但是我的循环显示为...
1->数字
2->数字
.
.
W->字母
O->字母
.
.
那么如何将这些字符组合为数字或单词?我的检查标准是否错误?


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?

推荐答案

感谢响应.我使用以下代码进行了解决..
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 = "";
}




试试这个:
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.


您需要将字符存储在变量中,然后在更改为其他类型时将其打印出来.下面是一个例子.尽管您可以轻松地添加标点或空格,但此示例不检查标点或空格.


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");

        }


这篇关于从字符中获取数字和单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆