字串(String)幾種處理方式

2022-09-06 16:51:10 字數 3656 閱讀 6862

1

int sdf = new

int[1

];2 sdf[0] = new

int[2];//

第一行例項化列並給它宣告2個位置

3 sdf[0][0] = 2

;4 sdf[0][1] = 5

;5 console.writeline(sdf[0][0

]);67//

一、比較字串89

//1.使用compare方法

1011

//int compare(string a,string b)比較字串a和b是否相等

12//

int compare(string a,string b,bool ignorcase)第三個引數表示是否忽略大小寫,true表示忽略

1314

string str1 = "

一念月初

";//

初的拼音開頭為 c

15string str2 = "

一念月末

";//

末的拼音開頭為 m

16//

c17//

m>c 1

1819 console.writeline(string.compare(str1, str2));//

-1,如果前面乙個較小,返回-1

20 console.writeline(string.compare(str2, str1));//

1,如果前面乙個較大,返回1

21 console.writeline(string.compare(str1, str1));//

0,如果兩個字串相等 返回0

2223

方法2425 console.writeline(str1.compareto(str2));//

-126

27//

3.eauals方法 ,返回true或false

2829

//方法一

30 console.writeline(str1.equals(str2));//

比較兩個字串是否相等,相等返回true

31//

方法二32 console.writeline(string

.equals(str1, str2));

3334

3536

//格式化字串

3738

string info = string.format("

現在就讀於班

", "

tom", "

yc37");

39console.writeline(info);

4041

//還可以用來格式化時間樣式

4243 datetime dt = datetime.now;//

獲取系統當前時間

44string time = string.format("

", dt); //

表示佔位符 d為樣式

4546

//自定義格式

4748 time = string.format("

", dt);//

mm表示月份,mm表示分鐘 hh為24小時制 hh為12小時制

49console.writeline(time);

5051

52//

擷取字串

53 console.writeline(str1.substring(1, 4));//

一念月初 從索引號為1的字元開始往後擷取4個字元

54 console.writeline(str2.substring(1));//

一念月末 從索引號為1的字元開始往後擷取到字串末尾

5556

5758

//分割字串

59//

split 引數是char,不是string

6061

//二、複製字串

62//

1.copy方法 public static string copy(string str)

6364

string s = string

.copy(str1);

65console.writeline(s);

6667

方法68//

void copyto(int sourceindex,

//從源字串第幾個字元開始copy,第乙個為0

69//

char destination,

//目標字串的char陣列

70//

int destinationindex,

//從目標字串char陣列的第幾個位置開始放

71//

int count

//一共複製多少個字元

72//)73

74char ch = new

char[100

];75

76//

將字串str1從索引1的位置開始的4個字串複製到ch中

77 str1.copyto(1, ch, 0, 4

);78

console.writeline(ch);

7980

81//

三、替換字串

82//

public string replace(char oldar,char newchar)

83//

public string replace(string oldstring,string new string)

8485

string a = "

one world,one dream";

86//

使用「*」替換「,」,並且賦值給b

87string b = a.replace('

,', '*'

);88

//使用「*」代替「o」,當原始字串中存在多個o時,會全部被替換

89console.writeline(b);

9091

92//

使用「one world 」替換"one world"

93string c = a.replace("

one world

", "

one world");

94console.writeline(c);

9596

97//

string

9899

//字串一旦宣告就不再可以改變,如果要替換裡面的值,要將替換了的放到乙個新的字串中

100//

例子:101

string ab = "

dfsdfsdf";

102103 ab.replace("

d", "m"

);104 console.writeline(ab);//

結果還是原來那個,因為字串一旦宣告就不可改變了

105106

string de = ab.replace("

d", "m"

);107 console.writeline(de);//

結果變了,因為它把改變的放到了新的乙個字串中

108//

或109 ab = ab.replace("

d", "

m"); //

直接替換掉原來的內容

字串問題與處理方式

可以用索引得到字串中指定下標的字元 string onestring hello char mychars mychars onestring 0 可以輸入看到得到的值為 h string onestring hello char mychars onestring.tochararray 轉化結果...

Highcharts中字串處理方式 1

字串資料也是圖表中一種常用的資料型別。在圖示中,字串型別資料可能出現在x值 y值,或者其他輔助性資訊中。當字串資料作為x值或者y值時,需要使用者特別注意。下面依次講解這兩種情況。從嚴格意義角度來說,highcharts是不允許字串作為x值。在實際使用中,我們會把x值作為節點的名稱name來使用。通過...

PHP中字串的處理方式

1.php中字串的處理占用程式設計範圍很多,要重點掌握 2.如果用字串函式來處理非字串型別的資料時會先將它轉成字串再做處理 3.字串也可以通過陣列下標的形式來訪問 通過大括號 也可以訪問,同樣陣列也支援 4.為了區別於陣列的訪問形式 可以選擇陣列使用來遍歷,而字串用 的形式 str strlen 1...