using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringDemo
{
class Program
{
static void Main(string[] args)
{
string str1 = "hello ";
string str2 = "rose_girls !";
//Compare
int result = String.Compare(str1, str2);
Console.WriteLine("compare result is " + result);
/**********************Concat(字符串连接)****************************/
//耗内存 耗时间
string contact = String.Concat(str1, str2);
Console.WriteLine("contact is: " + contact);
/**********************CopyTo(数据拷贝到指定源)****************************/
char[] tmpCha = new char[contact.Length];
contact.CopyTo(0, tmpCha, 0, contact.Length);
Console.Write("CopyTo is: ");
foreach (char c in tmpCha) {
Console.Write(c);
}
Console.WriteLine();
/**********************Format(格式化)****************************/
//默认格式化小数点后面保留两位小数
string format = string.Format("{0:C}", 15.34);
Console.WriteLine("format one dot is: " + format);
//截取会自动四舍五入 保留一位
format = string.Format("{0:C1}", 15.34);
Console.WriteLine("format two dot is: " + format);
/**********************IndexOf(找到单个字符出现的位置)****************************/
//获取在str中第一次出现l的位置
int index = str1.IndexOf('l');
Console.WriteLine("IndexOf index is: " + index);
/**********************IndexOfAny(找到当个字符或者字符数组的位置)****************************/
tmpCha = "ro".ToCharArray();
//在str2中找到第一次出现tmpcha中内容的位置
index = str2.IndexOfAny(tmpCha);
Console.WriteLine("IndexOf IndexOfAny is: " + index);
/**********************Insert(在指定位置插入字符串)****************************/
//在str2中插入"!!"字符串,返回插入后的字符串 不会改变str2本身的值
string tmpStr = str2.Insert(str2.Length - 1, "!!");
Console.WriteLine("Insert is: " + str2);
Console.WriteLine("Insert return is: " + tmpStr);
/**********************Join(合并字符串)****************************/
string join = string.Join("!","bbb","aaa","ccc");
Console.WriteLine("Join is: " + join);
/**********************PadLeft、PadRight(字符串左右填充)****************************/
//30:str2字符串的总长度 ‘l':用来填充的字符
string padLeft = str2.PadLeft(30, 'l');
string padRight = str2.PadRight(100, 'r');
Console.WriteLine("PadLeft is " + str2 + " " + padLeft);
/**********************Replace(字符串替换)****************************/
tmpStr = str1.Replace('l', 'L');
Console.WriteLine("Replace is: " + tmpStr);
/**********************Split(字符串分离)****************************/
string[] split = str2.Split('_');
Console.WriteLine("Split is: " + str2 + " split[0] = " + split[0] + " split[1] = " + split[1]);
/**********************Substring(子字符串)****************************/
tmpStr = str2.Substring(0, str2.Length - 2);
Console.WriteLine("Substring is:" + str2 + " tmpStr: " + tmpStr);
/**********************ToUpper、ToLower(大小写转换)****************************/
tmpStr = str2.ToUpper();
Console.WriteLine("ToUpper is:" + tmpStr);
tmpStr = tmpStr.ToLower();
Console.WriteLine("ToLower is:" + tmpStr);
/**********************Trim(删除首尾空白)****************************/
tmpStr = " " + tmpStr + " ";
Console.WriteLine("tmpStr is:" + tmpStr);
Console.WriteLine("Trim is:" + tmpStr.Trim());
Console.ReadLine();
}
}
}