A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 右哼哼 中级黑马   /  2014-1-8 16:31  /  1387 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 右哼哼 于 2014-1-9 17:21 编辑

在一段字符串中找到几个数字,怎样将数字取出来??并打印出来??求解,谢谢啦

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

6 个回复

倒序浏览
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();
        }

评分

参与人数 1技术分 +1 收起 理由
陈福军 + 1

查看全部评分

回复 使用道具 举报 1 0
如下代码,希望对你有所帮助:
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]);
                        }
                }               
        }

}

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

回复 使用道具 举报

        /* 从一段文本中提取所有的数字。*/
        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();

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

回复 使用道具 举报
不给代码。进行遍历。就可以了。
回复 使用道具 举报
同意楼上,逐个字符进行判断,char类型有可以判断字符类型的静态方法,例如判断数字类型的字符可以用
bool Char.IsDigit(char c);
回复 使用道具 举报
你可以用正则表达式尝试一下,用分组,然后全局匹配提取出来
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马