日期:2014-05-19  浏览次数:20435 次

关于字符串的问题(菜鸟问题)
1、有个TextBox.text

  protected   void   Button1_Click(object   sender,   EventArgs   e)
{
    在此希望判断出   TextBox.text   中的字符串大于4,小于15;
}
谢谢指点!

2、谢谢高手帮忙解释   以下语句中   引号使用的问题     ----有点晕
Response.Write( " <script   language= 'javascript '> window.open( 'StudyDetail.aspx?id= "   +   id   +   " ', '_blank '); </script> ");

  Response.Write( " <script> alert(\ "欢迎管理员\ "); </script> ");

------解决方案--------------------
1.if(textbox.text.length> 14 && textbox.text.length <15){}
2.response.write( " ")----> 所有在 " "内的属性值均用 ' '-----> 遇到连接变量,用 "+变量名+ "
" "内也康攸用 " ",不过要先转义\ "
------解决方案--------------------
protected void Button1_Click1(object sender, EventArgs e)
{
bool ret = false;
int i = 0;
if (int.TryParse(TextBox.Text, out i))
{
if (i > 4 && i < 15)
{
ret = true;
}
}
//ret就是你要的值
}
------解决方案--------------------
对于字符长度的判断 应该要 判断 中文及全角字符 这些是站2个字节的
可以使用下面的函数 获取
/// <summary>
/// 检测含有中文字符串的实际长度
/// </summary>
/// <param name= "str "> 字符串 </param>
public static int cnLenth(string str)
{
System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
byte[] b = n.GetBytes(str);
int l = 0; // l 为字符串之实际长度
for (int i = 0; i <= b.Length - 1; i++)
{
if (b[i] == 63) //判断是否为汉字或全脚符号
{
l++;
}
l++;
}
return l;
}