黑马程序员技术交流社区

标题: 怎样将字符串中的数字取出来 [打印本页]

作者: 右哼哼    时间: 2014-1-8 16:31
标题: 怎样将字符串中的数字取出来
本帖最后由 右哼哼 于 2014-1-9 17:21 编辑

在一段字符串中找到几个数字,怎样将数字取出来??并打印出来??求解,谢谢啦
作者: 一席倾城    时间: 2014-1-8 16:58
static void Main(string[] args)
        {
            Console.WriteLine("请输入要提取的字符串:");
            string str = Console.ReadLine();        
            string num = null;
            foreach (char item in str)
            {
                if (item >= 48 && item <= 58)//用ascall码表示数字1到9
                {
                    num += item;
                }
            }            
            Console.WriteLine(num);
           
            Console.ReadKey();
        }
作者: 王新年    时间: 2014-1-8 17:16
如下代码,希望对你有所帮助:
public class Test4 {

        public static void main(String[] args) {
                // TODO 自动生成的方法存根
                prints("wang12ynn6");

        }
        public static void prints(String st){
                char[] chs=st.toCharArray();
                for(int i=0;i<chs.length;i++){               
                        if(chs[i]>='0'&&chs[i]<='9'){
                                System.out.println(chs[i]);
                        }
                }               
        }

}
作者: 涵风    时间: 2014-1-8 17:59

        /* 从一段文本中提取所有的数字。*/
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一段文字");
            string input = Console.ReadLine();//输出文字
            char[] ch = input.ToCharArray();//将字符串复制到新的数组中
            int result = -1;//定义初始值
            for (int i = 0; i < input.Length;i++ )//遍历循环
            {
                switch (ch[i])//可能出现的情况
                {
                    case'0':
                        result = 0;
                        break;
                    case'1':
                        result = 1;
                        break;
                    case '2':
                        result = 2;
                        break;
                    case '3':
                        result = 3;
                        break;
                    case '4':
                        result = 4;
                        break;
                    case '5':
                        result = 5;
                        break;
                    case '6':
                        result = 6;
                        break;
                    case '7':
                        result = 7;
                        break;
                    case '8':
                        result = 8;
                        break;
                    case '9':
                        result = 9;
                        break;
                    default:
                        continue;
                }
                if (result != -1)//结果判断输出
                {
                    Console.Write(ch[i]);
                }
            }
            Console.ReadKey();
作者: 茹化肖    时间: 2014-1-8 21:45
不给代码。进行遍历。就可以了。
作者: yuanlianxi03    时间: 2014-1-8 22:39
同意楼上,逐个字符进行判断,char类型有可以判断字符类型的静态方法,例如判断数字类型的字符可以用
bool Char.IsDigit(char c);
作者: 念~    时间: 2014-1-9 09:18
你可以用正则表达式尝试一下,用分组,然后全局匹配提取出来




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2