C#的String类的IsNullOrEmpty方法

来源:互联网 发布:绿色建筑评价软件 编辑:程序博客网 时间:2024/04/27 15:07
// This example demonstrates the String.IsNullOrEmpty() method
using System;

class Sample 
{
  public static void Main() 
  {
  string s1 = "abcd";
  string s2 = "";
  string s3 = null;

  Console.WriteLine("String s1 {0}.", Test(s1));
  Console.WriteLine("String s2 {0}.", Test(s2));
  Console.WriteLine("String s3 {0}.", Test(s3));
  }

  public static String Test(string s)
  {
  if (String.IsNullOrEmpty(s) == true) 
  return "is null or empty";
  else
  return String.Format("(/"{0}/") is not null or empty", s);
  }
}
/*
This example produces the following results:

String s1 ("abcd") is not null or empty.
String s2 is null or empty.
String s3 is null or empty.

*/

原创粉丝点击