?string的學(xué)習(xí)
?* ?正則表達(dá)式
?* ?比如說:
?* ?賬號(hào)驗(yàn)證(包含英文字母,特殊符號(hào),數(shù)字,6位以上長(zhǎng)度);
?* ?郵箱驗(yàn)證
?* ?電話驗(yàn)證
class MainClass
{
/// <summary>
/// 獲取字符串長(zhǎng)度
/// </summary>
public void Test1(){
//無論英文,特殊符號(hào)還是中文,長(zhǎng)度都是1個(gè)字節(jié).
string s = "你好中國xxx,";
Console.WriteLine("字符串長(zhǎng)度為:" + s.Length);
}
/// <summary>
/// 字符串中查找字符串
/// </summary>
public void Test2(){
//返回-1證明查不到
//如果能查到,返回的是該字符或者字符串在字符串中索引位置
string s = "就是HFDJasdgajSGDasjgdhagshjdg不方便大家就卡死的";
int i = s.IndexOf('F',3,1);
Console.WriteLine (i);
}
/// <summary>
/// 字符串提取,截取指定范圍內(nèi)的字符串
/// </summary>
public void Test3(){
string s = "你好嗎,我的母親中國好棒!";
string temp = s.Substring(0,9);
Console.WriteLine (temp);
}
/// <summary>
/// 字符串替換
/// </summary>
public void Test4(){
string s = "淫露,你好你媽個(gè)x,草曹操肏";
string newStr = s.Replace("淫","*");
Console.WriteLine (newStr);
//分幾種:1.親戚
// string pattern = @"[淫銀癮]|[草曹操肏]|[爹媽姑舅爺]";
// string newStr = Regex.Replace (s, pattern, "*");
// Console.WriteLine (newStr);
}
/// <summary>
/// 字符串插入(指定位置)
/// </summary>
public void Test5(){
string s = "你好,中國";
string newStr = s.Insert(0,"china");
Console.WriteLine (newStr);
}
/// <summary>
/// 判斷字符串以什么什么結(jié)尾
/// </summary>
public void Test6(){
string s = "你好,中國";
bool b = s.EndsWith("中國");
if (b) {
Console.WriteLine ("是以中國結(jié)束");
}
}
/// <summary>
/// 字符串按照索引位置移除
/// </summary>
public void Test7(){
string s = "你好,中國";
string newStr = s.Remove(1,1);
Console.WriteLine (newStr);
}
/// <summary>
/// 字符串拼接
/// </summary>
public void Test8(){
string s = "你好";
s += "中國";
Console.WriteLine (s);
}
/// <summary>
/// 判斷字符串是否相等
/// </summary>
public void Test9(){
string s1 = "你好";
string s2 = "你好1";
string s3 = "你好2";
// if (s1 == s2) {
// Console.WriteLine ("相等的字符串");
// }
if(string.Equals (s1, s2)){
Console.WriteLine ("字符串s1和s2相等");
}
}
/// <summary>
/// 字符串轉(zhuǎn)換值類型
/// </summary>
public void Test10(){
string s = "123.";
// int i = int.Parse(s);
int result = 0;
bool l = int.TryParse (s, out result);
if (l) {
Console.WriteLine ("轉(zhuǎn)換成功!~" + result);
} else {
throw new Exception ("傳入數(shù)據(jù)包含非法字符,請(qǐng)?zhí)幚?");
}
}
//練習(xí)1:判斷字符串lm13142005@qq.com是否為合法郵箱
string email = "lm13142005qq.@com";
int indexAt = -1;
int indexDot = -1;
indexAt = email.IndexOf ("@");
indexDot = email.IndexOf (".");
if (indexAt != -1 && indexDot != -1) {
if (indexAt < indexDot) {
Console.WriteLine ("輸入email!");
}
}
匹配郵箱xxx@xxx.xxx
string email = "l@qqq.com";
string pattern = @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+";
if (Regex.IsMatch (email, pattern)) {
Console.WriteLine ("合法郵箱");
} else {
Console.WriteLine ("不合法郵箱");
}
/*
?* ?學(xué)習(xí)stringbuilder:目的解決拼接字符串產(chǎn)生的滯留性。
?* string類有不可改變性,每次執(zhí)行字符串拼接的時(shí)候,實(shí)際上都會(huì)產(chǎn)生一個(gè)新的字符串對(duì)象
?* stringbuilder類解決了對(duì)字符串重復(fù)修改過程中產(chǎn)生大量對(duì)象的問題
?* 初始化帶有capacity來控制容量大小,并且允許我們修改容量的大小
如果初始化是默認(rèn)構(gòu)造函數(shù)初始化的sb對(duì)象,那么默認(rèn)capacity的大小為16
C#5.0版本capacity成倍增長(zhǎng)
C#4.0版本之下的,拼接長(zhǎng)度超出容量的時(shí)候翻一倍,如果沒超過就是默認(rèn)值16
?*?
public static void Main (string[] args)
{
// 初始化帶有capacity來控制容量大小,并且允許我們修改容量的大小
// 如果初始化是默認(rèn)構(gòu)造函數(shù)初始化的sb對(duì)象,那么默認(rèn)capacity的大小為16
// C#5.0版本capacity成倍增長(zhǎng)
// C#4.0版本之下的,拼接長(zhǎng)度超出容量的時(shí)候翻一倍,如果沒超過就是默認(rèn)值16
// StringBuilder sb=new StringBuilder();
// string a = "你好";
// string b = "中國";
//
// sb.Append (a);
// sb.Append (b);
// //結(jié)論
// //容量大小沒有超過64,實(shí)際容量大小為64
// //容量>64就是實(shí)際容量大小
// Console.WriteLine ("沒拼接之前sb的容量為:{0}",sb.Capacity);
// Console.WriteLine ("沒拼接之前sb的長(zhǎng)度為:{0}\"",sb.Length);
//C#5.0 下面這種初始化方式,容量大小取決于初始化字符串的長(zhǎng)度
//C#5.0 以下跟上面情況相同
// string s = "你好";?
// StringBuilder sb=new StringBuilder();
// Console.WriteLine (sb.Capacity);
// StringBuilder sb=new StringBuilder("雙擊,");
// sb.Append ("{0}", 666);
// Console.WriteLine (sb.ToString());
string s="你好,中國";
StringBuilder sb=new StringBuilder();
sb.Append (s);
sb.Insert (1, "adf");
Console.WriteLine (sb.ToString());
sb.Remove (1, 1);
Console.WriteLine (sb.ToString());
sb.Replace ("好", "*");
Console.WriteLine (sb.ToString());
Console.ReadLine ();
//
}