string a="92fm92%3un#fm983&ufm";
怎么提取数字和字母字符输出

解决方案 »

  1.   


    string a="92fm92%3un#fm983&ufm"; 
    string output = Regex.Replace(a,"[^0-9a-zA-Z]+",""); // ="92fm923unfm983ufm"; 
    或者
    MatchCollection mc = Regex.Matches(a,"[0-9a-zA-Z]+");
    foreach(Match m in mc)
    {
        Console.WriteLine(m.Value); // 92fm92,3un,fm983,ufm 
    }
      

  2.   

    Regex类在System.Text.RegularExpressions命名空间里,正则表达式很好用,很强大
      

  3.   

    可以正则表达式,也可以自己用substring 截取,需要自己写判断语句  这个 方法比较笨  嘿嘿
      

  4.   


    using System;
    using System.Text.RegularExpressions;namespace WondowsConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                string text = "abc##$%25";
                string pattern = "[^0-9a-zA-Z]+";
                Console.WriteLine(Regex.Replace(text,pattern,""));
            }
        }
    }
      

  5.   


            static void Main(string[] args)
            {
                string text = "a&*bc##$%25";
                string pattern = "[0-9a-zA-Z]+";            Regex reg = new Regex(pattern);
                foreach (Match m in reg.Matches(text))
                {
                    Console.WriteLine(m.Value); 
                }
            }输出:
    a
    bc
    25
      

  6.   

    static void Main(string[] args)
            {
               string text = "a&*bc##$%25";
                Regex reg = new Regex(@"\d{1,}|[a-zA-Z]{1,}");
                foreach (Match m in reg.Matches(text))
                {
                    Response.Write(m.Value+"<br/>"); 
                }    
    }
      

  7.   

    如果匹配字符和数字混合可以用9楼的正则,如果分别匹配字符和数字,用10楼的,三种情况全部匹配
    (@"\d{1,}|[a-zA-Z]{1,}|\w{1,}";
      

  8.   

    1. 逐个字符处理
    2. 正则表达式,楼主的问题比较简单,有点杀鸡用牛刀
    3. 正解: Convert.ToInt32,
       Convert.ToInt32 方法 (String, IFormatProvider),可以提供格式