正則表達式,字符串常見API

?string的學習

?* ?正則表達式

?* ?比如說:

?* ?賬號驗證(包含英文字母,特殊符號,數(shù)字,6位以上長度);

?* ?郵箱驗證

?* ?電話驗證

class MainClass

{

/// <summary>

/// 獲取字符串長度

/// </summary>

public void Test1(){

//無論英文,特殊符號還是中文,長度都是1個字節(jié).

string s = "你好中國xxx,";

Console.WriteLine("字符串長度為:" + 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 = "淫露,你好你媽個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ù)包含非法字符,請?zhí)幚?");

}


}

//練習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 ("不合法郵箱");

}

/*

?* ?學習stringbuilder:目的解決拼接字符串產(chǎn)生的滯留性。

?* string類有不可改變性,每次執(zhí)行字符串拼接的時候,實際上都會產(chǎn)生一個新的字符串對象

?* stringbuilder類解決了對字符串重復修改過程中產(chǎn)生大量對象的問題

?* 初始化帶有capacity來控制容量大小,并且允許我們修改容量的大小

如果初始化是默認構(gòu)造函數(shù)初始化的sb對象,那么默認capacity的大小為16

C#5.0版本capacity成倍增長

C#4.0版本之下的,拼接長度超出容量的時候翻一倍,如果沒超過就是默認值16

?*?


public static void Main (string[] args)

{

// 初始化帶有capacity來控制容量大小,并且允許我們修改容量的大小

// 如果初始化是默認構(gòu)造函數(shù)初始化的sb對象,那么默認capacity的大小為16

// C#5.0版本capacity成倍增長

// C#4.0版本之下的,拼接長度超出容量的時候翻一倍,如果沒超過就是默認值16

// StringBuilder sb=new StringBuilder();

// string a = "你好";

// string b = "中國";

//

// sb.Append (a);

// sb.Append (b);

// //結(jié)論

// //容量大小沒有超過64,實際容量大小為64

// //容量>64就是實際容量大小

// Console.WriteLine ("沒拼接之前sb的容量為:{0}",sb.Capacity);

// Console.WriteLine ("沒拼接之前sb的長度為:{0}\"",sb.Length);

//C#5.0 下面這種初始化方式,容量大小取決于初始化字符串的長度

//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 ();

//


}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容