string s = string.empty;
string s = "";
string s = null;
第一种第一一个指向空字符串的字符串变量, s不占用存储空间.s变量可以使用,比如s.length
.
第二种定义一个指向""的空字符串, 占用一个字节存储空间,因为转义符代表字符串结束.s变量可以使用,比如s.length
第三种定义一个指不向任何一个存储空间的变量,s不可以使用
字符串连接:
--------------------------------------------------------------------------------
protected void button1_click(object sender, eventargs e)
{
string str1="abcd", str2="efg";
str1 += str2;
response.write(str1); //abcdefg
}
--------------------------------------------------------------------------------
从字符串中提取字符:
--------------------------------------------------------------------------------
protected void button1_click(object sender, eventargs e)
{
string str = "abcdefg";
char c = str[1]; //read only
response.write(c); //b
response.write(str[str.length-1]); //g
}
--------------------------------------------------------------------------------
从字符串中遍历字符:
--------------------------------------------------------------------------------
protected void button1_click(object sender, eventargs e)
{
string str = "abcdefg";
for (int i = 0; i < str.length; i++)
{
response.write(str[i] + "<br/>");
}
foreach (char c in str)
{
response.write("<hr/>" + c);
}
}
--------------------------------------------------------------------------------
从字符编码转义到字符:
--------------------------------------------------------------------------------
protected void button1_click(object sender, eventargs e)
{
string str = "u4e07u4e00";
response.write(str); //万一
}--------------------------------------------------------------------------------
首页 1 2 末页