Finishing: C # Common string operations, common numeric type conversion

A string operation

1. String connection

// specified all array elements are spliced to a string 
String [] ARR = { " A " , " B " , " C " };
 String .Concat (ARR); 

// use hyphens as a string array splice 
String .join ( " , " , ARR);     // output "A, B, C"

2. Extract the string of characters

// Get a character string char 
String STR = " the Hello " ;
 char CH STR = [ . 4 ];     // CH = 'O' 

// string taken care regardless of the tail from 0 
String newStr = STR. substring ( 0 , . 1 );     // newStr = "H"

3. string case conversion

String STR = " Abc " ; 

// uppercase 
str.ToUpper ();     // the ABC 

// lowercase 
str.ToLower     // ABC

4. Split string is an array of strings

string str = "A,B,C";
string[] arr = str.Split(',');    //arr {"A","B","C"}

 5. Replace character

// of str "_" is replaced with "," 
String str = " A_B_C " ; 
str.replace ( ' _ ' , ' , " );     // str =" A, B, C "

6. Remove both spaces string

string str = "A B C";
string newStr = str.Trim();    //newStr = "ABC"

7. matching index

string str = "ABCDE";
int index = str.IndexOf('A');    //index = 0

8. String filling, filled

String STR = " 666 " ;
 // left padding 
str.PadLeft ( . 6 , ' 0 ' );     // Returns "000666" 

// right padding 
str.PadRight ( . 6 , ' 0 ' );     // Returns "666000"

9. string formatting (static method)

string str = string.Format("{0}---{1}",pig,dog);    //str = "pig---dog"

10. Analyzing the specified character string contains

string a = "I am Mike";
string b = "Mike";
bool result = a.Contains(b);    //result = true

Second, the value of common type conversion

1. int type to string

int a = 250;
string b = a.ToString();    //b = "250"

2. string to int

string a = "250";
int result;
//方法1
result = int.Parse(a);

//方法2
result = Convert.ToInt32(a);

//方法3
int.TryParse(a,out result);

 

Guess you like

Origin www.cnblogs.com/mikeyu/p/11094997.html