A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 庞海瑞 中级黑马   /  2013-8-7 15:33  /  1386 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  
 
 
  判断空字符串: 
 string test1 = ""; 
 string test2 = string.Empty;
  string test3 = null;
  Console.WriteLine("test1 = \"\"" +" ");
  Console.WriteLine("test2 = string.Empty"  "</br>");
  Console.WriteLine("test3 = null" + "</br>");
  if (test1 == "")
  Console.WriteLine("(test1 == \"\") is :True"+"</br>");
  if(test2 == string.Empty)
  Console.WriteLine("(test2 == string.Empty) is:True" + "</br>");
  if(test1 == string.Empty)
  Console.WriteLine("(test1 == string.Empty) is: True" + "</br>");
  if(test2 == "")
  Console.WriteLine("(test2 == \"\") is: True" + "</br>");
  if(test1 == test2)
  Console.WriteLine("(test1 == test2) is: True" + "</br>");
  if(test3 == null)
  Console.WriteLine("(test3 == null) is: True" + "</br>");
  if (test1 != null)
  Console.WriteLine("(test1 != null) is : True" + "</br>");
  if (test2 != null)
  Console.WriteLine("(test2 != null) is : True" + "</br>");
  if(test1.Length ==0)
  Console.WriteLine("(test1.Length ==0) is: True" + "</br>");
  if(test2.Length==0)
  Console.WriteLine("(test2.Length==0) is : True" + "</br>");
  //if(test3.Length == 0)//Error,null不能用Length来进行判断为空
  if(string.IsNullOrEmpty(test1))
  Console.WriteLine("(string.IsNullOrEmpty(test1)) is :True" + "</br>");
  if (string.IsNullOrEmpty(test2))
  Console.WriteLine("(string.IsNullOrEmpty(test2)) is :True" + "</br>");
  if (string.IsNullOrEmpty(test3))
  Console.WriteLine("(string.IsNullOrEmpty(test3)) is :True" + "</br>");
  输出:  test1 = "" 
 test2 = string.Empty
  test3 = null
  (test1 == "") is :True
  (test2 == string.Empty) is:True
  (test1 == string.Empty) is: True
   (test2 == "") is: True
  (test1 == test2) is: True
  (test3 == null) is: True
  (test1 != null) is : True
  (test2 != null) is : True
  (test1.Length ==0) is: True
  (test2.Length==0) is : True
  (string.IsNullOrEmpty(test1)) is :True
  (string.IsNullOrEmpty(test2)) is :True
  (string.IsNullOrEmpty(test3)) is :True
  因此,判断字符串为空最通用的方法就是IsNullOrEmpty()无论是"", string.Empty还是null。
      如果字符串初始化为null,则不能使用test3.Length == 0进行判断。对于"",和string.Empty 使用s.Length == 0,s == string.Empty 和s == ""都可以,这里面不讨论性能问题。
比较字符串相等
  Equals() 和运算符 ==
   分割字符串
  Split 方法将字符串分隔后返回字符串数组。
  Split使用方法一:
  string str = "abcdefghigkceofuecto";
  string[] sArray = str.Split('c');
  foreach (string i in sArray)
  Console.WriteLine(i.ToString()+"</br>");
  输出下面的结果:
  ab
  defghigk
  eofue
  to
  Split使用方法二:
  我们看到了结果是以一个指定的字符进行的分割。使用另一种构造方法对多个字符进行分割:
  string str = "abcdefghigkceofuecto";
  string[] sArray = str.Split(new char[3] { 'h', 'i', 'g' });
  foreach (string i in sArray)
  Console.WriteLine(i.ToString()+"</br>");
  输出:
  abcdef
  kceofuecto
  使用正则表达式:
  使用正则表达式,可以过滤掉一些不是很规则的字符。
  string str = "everybody***************Welcome*****to***china*!";
  string[] sArray = System.Text.RegularExpressions.Regex.Split(str, @"[*]+");
  foreach (string i in sArray)
  Console.WriteLine(i.ToString()+"</br>");
  输出:
  everybody
  Welcome
  to
  china
  !
  字符串截取
  string myString = "Hello Word!";
  //Substring()在C#中有两个重载函数
  //分别如下示例
  string subString1 = myString.Substring(0);
  //如果传入参数为一个长整, 且大于等于0,
  //则以这个长整的位置为起始,
  //截取之后余下所有作为字串.
  //如若传入值小于0,
  //系统会抛出ArgumentOutOfRange异常
  //表明参数范围出界
  string subString2 = myString.Substring(0, 5);
  //如果传入了两个长整参数,
  //前一个为参数子串在原串的起始位置
  //后一个参数为子串的长度
  //如不合条件同样出现上述异常
  Console.WriteLine(subString1);//Hello Word!
  Console.WriteLine(subString2);//Hello
格式化字符串
  格式化数字
  格式字符 说明和关联属性
  ________________________________________
  c、C 货币格式。
  d、D 十进制格式。
  e、E 科学计数(指数)格式。
  f、F 固定点格式。
  g、G 常规格式。
  n、N 数字格式。
  P、P 百分比
  r、R 往返格式,确保将已转换成字符串的数字转换回数字时具有与原数字相同的值。
  x、X 十六进制格式。
  ________________________________________
  double val=Math.PI;
  Console.WriteLine(val.ToString( )); //displays 3.14159265358979
  Console.WriteLine(val.ToString("E"));//displays 3.141593E+000
  Console.WriteLine(val.ToString("F3");//displays 3.142
  int val=65535;
  Console.WriteLine(val.ToString("x")); //displays ffff
  Console.WriteLine(val.ToString("X")); //displays FFFF
  Single val=0.123F;
  Console.WriteLine(val.ToString("p")); //displays 12.30 %
  Console.WriteLine(val.ToString("p1")); //displays 12.3 %
  格式化日期
  d 短日期模式
  表示由当前 ShortDatePattern 属性定义的自定义 DateTime 格式字符串。
  例如,用于固定区域性的自定义格式字符串为“MM/dd/yyyy”。
  G 常规日期/时间模式(短时间)
  表示短日期 (d) 和短时间 (t) 模式的组合,由空格分隔。
  G 常规日期/时间模式(长时间)
  表示短日期 (d) 和长时间 (T) 模式的组合,由空格分隔。
  M 或 m 月日模式
  表示由当前 MonthDayPattern 属性定义的自定义 DateTime 格式字符串。
  例如,用于固定区域性的自定义格式字符串为“MMMM dd”。
  R 或 r RFC1123 模式
  表示由当前 RFC1123Pattern 属性定义的自定义 DateTime 格式字符串。该模式是定义的标准,并且属性是只读的。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。
  定义格式字符串为“ddd, dd MMM yyyy HH':'mm':'ss 'GMT'”。
  格式化不会修改正在格式化的 DateTime 对象的值。因此,应用程序在使用此格式说明符之前必须将该值转换为协调世界时 (UTC)。
  T 长时间模式
  表示由当前 LongTimePattern 属性定义的自定义 DateTime 格式字符串。
  例如,用于固定区域性的自定义格式字符串为“HH:mm:ss”。
  U 通用的可排序日期/时间模式
  表示由当前 UniversalSortableDateTimePattern 属性定义的自定义 DateTime 格式字符串。此模式是定义的标准,并且属性是只读的。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。
  自定义格式字符串为“yyyy'-'MM'-'dd HH':'mm':'ss'Z'”。
  格式化日期和时间时不进行时区转换。因此,应用程序在使用此格式说明符之前必须将本地日期和时间转换为协调世界时 (UTC)。
DateTime dt = DateTime.Now;  String date;  date = dt.ToString("d",DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//07/22/2009  date = dt.ToString("G", DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//07/22/2009 14:54:37  date = dt.ToString("r", DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//Wed, 22 Jul 2009 14:54:37 GMT  date = dt.ToString("T", DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//14:54:37  date = dt.ToString("u", DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//2009-07-22 14:54:37Z  date = dt.ToString("dd-MM-yyyy", DateTimeFormatInfo.InvariantInfo);  Console.WriteLine(date + "</br>");//22-07-2009
  

1 个回复

倒序浏览
{:soso_e179:}
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马