string s=“12345”如何判断字符数? string s=“12345”如何判断字符数? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Encoding.Default.GetByteCount(""); - -!LZ这个都不晓得啊...s.Length属性 int count = 0; string str = "1234dbbb5a"; for (int i=0;i<str.Length;i++) { if (str[i]>='0'&&str[i]<='9') { count++; } } MessageBox.Show("数字个数为:"+count.ToString()); s.Length,不过怀疑楼主不是这个意思? 这个吧,如果是中文跟 s.Length 就会有区别了 s.Length 这应该是你想要的 就是用s.Length属性.这个属性就是判断字符个数的,不管中文还是西文. 没必要这么麻烦,应该Encoding.Default.GetByteCount("");就可以的 恩 全都正解 .length 或者笨方法一个个字符遍历 用一个 累加器进行计数 s.Lengthor Encoding.Default.GetByteCount(""); 其实楼主的这个问题无比的复杂,里面牵扯到牛顿1,2,3定律,和哥德巴赫擦猜想!如果简单的回家 str.Length 的话,就太失败了! s.Length这个上面都说了。LZ是不是想判断是不是数字??使用 TryParsehttp://www.cnblogs.com/legend_sun/archive/2008/12/23/1360533.html bool isnumeric =false; System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); //从字符串中提取字节--转换为字节长度 byte[] bytestr = ascii.GetBytes(str); for (int i = 0; i < bytestr.Length; i++) { c = bytestr[i]; // if ((c >= 48 && c <= 57) || c == 46)//如果为字符数 { isnumeric = true; } } 如果要数字就先将字符串转换成ACCII码数组 然后根据大小去比较0-9的ACCII码 完事了 C#对象数组的问题,求助 wcf调用出错 Delegate和event (重赏)SHDocvw.WebBrowser.Document转换mshtml.HTMLDocument的问题 急!求一正则表达式,谢谢! 如何改分辨率~~~ 高手请进! 我想找一些JS函数的资料 求助~~~~~~~~~~~在c#中如何上传图片到 sql server中的image字段中!!!! 跪求!~~~~~~ C# winform不同程序集之间如何共享数据 关于listview 乱码问题
LZ这个都不晓得啊...
s.Length属性
int count = 0;
string str = "1234dbbb5a";
for (int i=0;i<str.Length;i++)
{
if (str[i]>='0'&&str[i]<='9')
{
count++;
}
}
MessageBox.Show("数字个数为:"+count.ToString());
这个吧,如果是中文跟 s.Length 就会有区别了
没必要这么麻烦,应该Encoding.Default.GetByteCount("");
就可以的
or
Encoding.Default.GetByteCount("");
这个上面都说了。
LZ是不是想判断是不是数字??
使用 TryParse
http://www.cnblogs.com/legend_sun/archive/2008/12/23/1360533.html
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
//从字符串中提取字节--转换为字节长度
byte[] bytestr = ascii.GetBytes(str);
for (int i = 0; i < bytestr.Length; i++)
{
c = bytestr[i];
//
if ((c >= 48 && c <= 57) || c == 46)//如果为字符数
{
isnumeric = true;
}
}