字符串函数的使用
1.字符串函数的专讲
//①给出一个字符串,首先要测试出这个字符串的长度
//用length 还可以"" 字符串的长度,null则无法测试出长度
//因为null不指定存储空间,所以无法测试长度
//length 1个汉字占一个字节
string s = "abcdefg";
Console.WriteLine(s.Length);
//②比较两个字符串的范围,忽略大小写,字符顺序是否一样
//s.Equals(new string,StringComparison.OrdinalIgnoreCase)
if (s.Equals("abcdEFG", StringComparison.OrdinalIgnoreCase))
Console.WriteLine("这是两个一样的字符串");
else
Console.WriteLine("两个字符串不一致");
//③比较两个字符串大小写是否一样 s.Equals(newstring)
if (s.Equals("abcdefg")) //两个字符串如果相同则返回True值
{
Console.WriteLine("两个字符串一样");
}
else
{
Console.WriteLine("两个字符串不一样");
}
//④字符串劈开split函数
//1)简单的字符串劈开函数 s1.Split('|')
★分开的是数组,而不是字符串
string s1 = "aa|bb|c";
string[] f = s1.Split('|'); //是'' 而不是" "
foreach (string e in f)
Console.Write(e);
Console.WriteLine();
//2)劈开一个字符,组成字符串数组
//RemoveEmptyEntries移除空的字符串数组元素 none 保留空的字符串数组元素。
string[] f1 = s1.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string e1 in f1)
Console.WriteLine(e1);
//3)string[] f3 = s.Split('我') 只能劈开单个字符
string[] f2 = s1.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string e2 in f2)
Console.WriteLine(e2);
//⑤敏感字符的判断
//1)字符串中是否有子串 是否有敏感字符串 s.Contains("社会") || s.Contains("和谐")
string str1 = "我们的社会真和谐啊!!!";
if (str1.Contains("社会") || str1.Contains("和谐"))
Console.WriteLine("禁止出现敏感词汇");
//2)敏感词汇进行*掩藏
string str = "社会真和谐啊,构造和谐社会!!!";
if (str.Contains("和谐"))
{
char[] temp = str.ToCharArray();
for (int i = 0; i < f.Length; i++)
{
if (temp[i] == '和' && temp[i + 1] == '谐')
{
temp[i] = '*';
temp[i + 1] = '*';
}
}
string s3 = new string(temp);
Console.WriteLine(s3);
Console.ReadKey();
}
//⑥字符串大小写转换 ToLower() ToUpper()string strbs = "abcde";
Console.WriteLine(strbs.ToLower());//全部转换成小写的形式
Console.WriteLine(strbs.ToUpper());//全部转换成大写的形式
//⑦修剪字符串中出现的空格
string strspace = " aaa bbb ccc ";
Console.WriteLine("原始字符串是:{0}|", strspace);
Console.WriteLine("没有修剪之前的字符串长度,{0}", strspace.Length);
Console.WriteLine("\n");
Console.WriteLine("修剪两边字符串后是:{0}|", strspace.Trim());
Console.WriteLine("修剪两边字符串后的长度,{0}", strspace.Trim().Length);
Console.WriteLine("\n");
Console.WriteLine("修剪字符串开始位置后是,{0}|", strspace.TrimStart());
Console.WriteLine("修剪字符串开始位置后长度,{0}", strspace.TrimStart().Length);
Console.WriteLine("\n");
Console.WriteLine("修剪字符串结束位置后是,{0}|", strspace.TrimEnd());
Console.WriteLine("修剪字符串结束位置后长度,{0}", strspace.TrimEnd().Length);
//⑧字符串替换函数 s.Replace(oldstring,newstring ); 字符串替换
Console.WriteLine("\n");
string strreplace = "高山|长江|黄河|北京";
strreplace = strreplace.Replace("|", ",");
Console.WriteLine(strreplace); //高山,长江,黄河,北京
//⑨
//s.Substring(startIndex, length) 开始的索引 截取长度
string strsub = "高山|长江|黄河|北京";
strsub = strsub.Substring(3, 3);
Console.WriteLine(strsub);
//三元运算符 i==5?"是":"否"
int inti = 5;
string strsub2 = (inti == 5 ? "是" : "否");
Console.WriteLine(strsub2);
string strsub3 = "高山|长江";
strsub3 = strsub3.Substring(2, strsub3.Length - 2 > 5 ? 5 : strsub3.Length - 2);
// 或者 s = s.Substring(2,Math.Min(s.Length-2,5));
Console.WriteLine(strsub3);
Console.ReadKey();
Equals判断对象是否想等。
如果不在指向同一存储地址的话,那么肯定不想等,如果指向同一内存地址的话,再进行判断。
字符串进行判断的是两个对象的值,而不是对象。
// test t = new test();
t.Name = "a";
test t1 = new test();
t1.Name = "a";
t.Equals(t1);
class test
{
public string Name;
public override bool Equals(object obj)
{
return this.Name == ((test)obj).Name;
}
}
2.
(1)日期函数
DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(2).字串对比一般都用: if(str1==str2){ } , 但还有别的方法:
①string str1; str2
//语法: str1.EndsWith(str2); __检测字串str1 是否以字串str2 结尾,返回布尔值.如:
if(str1.EndsWith(str2)){ Response.Write("字串str1 是以"+str2+"结束的"); }
②
//语法:str1.Equals(str2); __检测字串str1 是否与字串str2 相等,返回布尔值,用法同
上.
③
//语法Equals(str1,str2); __检测字串str1 是否与字串str2 相等,返回布尔值,用法同
上.
(3).在指定的字符串中 插入函数Insert()
在字串中指定索引位插入指定字符。如:
str1.Insert(1,"字");在str1 的第二个字符处插入“字”,如果str1="中国",插入后
为“中字国”;
4.在字串左(或右)加空格或指定char 字符,使字串达到指定长度
PadLeft()、PadRight()
<%
string str1="中国人";
str1=str1.PadLeft(10,'1'); //无第二参数为加空格
Response.Write(str1); //结果为“1111111 中国人” , 字串长为10
%>