如何在一个字符串中判断是否有中文!例如:字符串(泸A12345) ,我怎么能判断第一个为中文

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.Text.RegularExpressions;public class MyClass
    {
    public static void Main()
    {
    try
    {
    Regex r = new Regex("[\u4e00-\u9fa5]");

    if(r.IsMatch("泸A12345"))
    {
    Console.WriteLine("有中文");
    }
    else
    {
    Console.WriteLine("无中文");
    }
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.ToString());
    }
    RL();
    }

    private static void WL(string text, params object[] args)
    {
    Console.WriteLine(text, args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break() 
    {
    System.Diagnostics.Debugger.Break();
    }
    }
      

  2.   


    <html>
    <head>
    <script language="javascript">
    function funcChina(){
    var obj = document.frm.txtName.value;
    if(/^[\u4e00-\u9fa5]+$/.test(obj))
    {
    alert("不能含有汉字!");
    return false;
    }
    return true;
    }
    </script>
    </head>
    <form name=frm>
    <body>
    <br>
    <input type=text name=txtName> <input type=button name=butTxt value="判断是否是汉字" onclick="funcChina()"><br>
    </form>
    </body>
    </html>你把程序稍做修改,也就是取出被判断的第一个字符,然后再通过funcChina()函数判断就可以了。
      

  3.   

    其实就在var obj = document.frm.txtName.value;语句的时候取第一个字符做为obj变量就可以了啊
      

  4.   

    using System;
    using System.Collections;
    using System.Text.RegularExpressions;public class MyClass
    {
    public static void Main()
    {
    Regex r = new Regex("[\u4e00-\u9fa5]");
    string input = "泸A12345";

    for(int i=0;i<input.Length;i++)
    {
    if(r.IsMatch(input[i].ToString()))
    {
    Console.WriteLine("第" + (i+1) + "个字是中文 -- " + input[i]);
    }
    else
    {
    Console.WriteLine("第" + (i+1) + "个字不是中文 -- " + input[i]);
    }
    }

    RL();
    }

    private static void WL(string text, params object[] args)
    {
    Console.WriteLine(text, args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break() 
    {
    System.Diagnostics.Debugger.Break();
    }
    }