字符串的应用---功能函数

//1. string 是System.String的别名, 字符串需要使用双引号引起来
string s = "www.devsiki.com";

//2. 获取字符串长度
int length = s.Length;

//3. 比较字符串是否一样 ,结果为相同
if (s == "www.devsiki.com")
{
Console.Write("相同");
}
else
{
Console.Write("不相同");
}

//4. 字符串连接,string创建的字符串实际上是一个不可变的数据类型,一旦对字符串对象进行了初始化,该字符串就不能改变内容了
//此例中实际上是创建了一个新的字符串,把旧字符串的内容复制到新字符串中。然后把新字符串的引用赋值为字符串的对象。
//(重复修改给定的字符串,效率会很低)
s = "http://" + s;

//5. 使用类似索引器的语法来取得字符串中的某个字符,索引从0开始,s[3]结果为p
char c = s[3]; //string t = s[3].ToString();

 

//6. 比较:比较字符串的内容,当两个字符串相等的时候,返回0,当s在字母表中的排序靠前的时候,返回-1, 否则返回1
string s = "www.devsiki.com";
int res = s.CompareTo("www.devsiki.com");//返回0

 

//7. 替换:用另一个字符或者字符串替换字符串中给定的字符或者字符串,也可以string newStr = s.Replace('.', '-');
string newStr = s.Replace(".", "----");//"www----devsiki----com"

 

//8. 拆分:在出现给定字符的地方,把字符串拆分称一个字符串数组
string[] strArray = s.Split('.');// "www","devsiki","com"

 

//9. 获取:在字符串中检索给定位置的子字符串, 从0开始算
string str = s.Substring(4);//"devsiki.com"

 

//10.大小写:把字符串转换成小写形式ToLower(),ToUpper()把字符串转换成大写形式
str = s.ToUpper();//"WWW.DEVSIKI.COM"

//首尾:删除首尾的空白

s = " www.devsiki.com ";
str = s.Trim();//"www.devsiki.com"

 

//11.CopyTo()方法,把字符串中指定的字符复制到一个数组中。Insert()把一个字符串插入另一个字符串的指定索引处。

 

 

//12.检索位置:取得字符串第一次出现某个给定字符串或者字符的位置,可以使用这个方法判断当前字符串是否包含一个子字符串,
//不包含,返回-1,包含会返回第一个字符的索引
s = "www.devsiki.com";
int index = s.IndexOf("devsiki");//4,就是指d的位置

//13.Format

// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"


// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

//use of thousands separator
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"


//Align numbers with spaces

String.Format("{0,10:0.0}", 123.4567); // " 123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

//custom format
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"
//some funny examples
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"
//number
String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"
//string
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"
String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime
 
Console.WriteLine("-------------------------------");
Console.WriteLine("First Name | Last Name  |   Age");
Console.WriteLine("-------------------------------");
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Bill", "Gates", 51));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Edna", "Parker", 114));
Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Johnny", "Depp", 44));
Console.WriteLine("-------------------------------");

Output string:

 -------------------------------
 First Name | Last Name  |   Age
 -------------------------------
 Bill       | Gates      |    51
 Edna       | Parker     |   114
 Johnny     | Depp       |    44
 -------------------------------
posted @   apple-hu  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示