1、字串(string)是字元(char)的唯讀陣列,字串具有不可變性
2、字串(string)與字元陣列(char)之間的轉換string text =
"嘿嘿,我在學習"
;char ch = text[1]
;//text[1] = "1"; 這個是不能改值的,所以說字串具有不可變性
ch =
'和';
//可以將字串轉換為字元陣列,然後將裡面的值更改
3、字串(string)大小寫轉換string text =
"嘿嘿,我在學習"
;char
chs = text.
tochararray()
;//字串轉換為字串陣列
string str = new string
(chs)
;//字元數組裝換為字串
得到字串的小寫形式:tolower()
得到字串的大寫形式:toupper()
忽略大小寫,返回bool型別:string.equals()
4、字串的分割:split()console.
writeline
("輸入第一門課程");
string str1 = console.
readline()
;str1=str1.
tolower()
;//str1轉換為小寫的字串
str1 = str1.
toupper()
;//str1轉換為大寫的字串
console.
writeline
("輸入第二門課程");
string str2 = console.
readline()
;bool result = str1.
equals
(str2, stringcomparison.ordinalignorecase)
;//忽略大小寫比較,返回bool型別
例子:字串分割後加字元:將「2019--------3------17」轉換為2023年3月17號static
void
main
(string[
] args)
;//設定字串要割捨的字元為空格和-
string[
] result = str.
split
(chs,stringsplitoptions.removeemptyentries)
;//字串str中的空格和-切割掉,並且去除切割後的空白項
}
5、其他型別轉換為字串型別:tostring()string date =
"2019--------3------17"
;char
chs = new char
;string[
] time = date.
split
(chs, stringsplitoptions.removeemptyentries)
;console.
writeline
("年月日"
, time[0]
, time[1]
, time[2]
);
6、字串替換:replace()int
nums = new int
;string st = nums.
tostring()
;console.
writeline
(st)
;//返回的是型別system.int32
console.
readkey()
;
7、檢視是否有子字串:contains()string name =
"卡迪夫卡"
; name = name.
replace
('卡'
,'你');
//將字串中的卡都替換成你
8、取子字串:substring()string name =
"卡迪夫卡"
;bool result = name.
contains
("卡迪");
//是否包含子字串,返回bool型別
9、 判斷字串是否以某個字串開始的:startswith(),是的話返回truestring str =
"我們都愛學習"
;str = str.
substring(2
,2);
//從下標為2的開始取,取兩個字元
str = str.
substring(4
);//從下標為4的開始擷取,擷取到最後乙個
10、 判斷字串是否以某個字串結束的:endswith(),是的話返回truestring str =
"我們都愛學習"
;bool result=str.
startswith
("我們");
//判斷字串是否以我們開始的,返回bool型別
11、取子字串第一次出現的位置:indexof()string str =
"我們都愛學習"
;bool result = str.
endswith
("學習");
//判斷字串是否以某個字串開始的
12、取子字串最後一次出現的位置:lastindex()string str =
"我們都愛學習,我愛學習"
;int index0=str.
indexof
("他");
//子字串第一次出現的位置的下標,從下標為0的開始找
int index2 = str.
indexof
("學",6
);//搜尋該子字串從指定的字元位置開始,也就是從下標為6的開始找
;int index4 = path.
lastindexof
("\\");
//獲取\最後一次出現位置的下標
13、在字串某個索引處插入字串:insert()
14、判斷字串是否為空,返回值為bool型別,空的時候返回true:isnullorempty()string st =
"嘿嘿,我在學習"
;st=st.
insert(3
,"他");
//在下標為3的後面插入他這個字串
15、移除字串:remove()string str ="";
bool result= string.
isnullorempty
(str)
;//檢測字串裡是否為空
16、切除字串前後空格string text =
"我們都只愛學習"
;string text2 = text.
remove(4
);//從第四個個開始刪除,後面的全部刪除
string text1 = text.
remove(2
,3);
//從某個地方開始移除,然後移除裡面的多少個,這個是從第二個開始刪除,刪除3個
17、字串的join方法:在字串陣列中間新增字元形成乙個字串string name =
" mark "
name = name.
trim()
;
例子: //把變成老馬|老牛|老花|老李
string[
] names =
;//在陣列之間插入|然後連起來形成乙個字串
string st=string.
join
("|"
, names)
;
C字串基礎
include include int main printf s n name mhm printf s n name2 12mhm,說明從低位址一直往高位址讀,一直讀到 0為止 printf p n name c68 printf p n name2 c66 通過以上倆位址,可以看出,name先...
C 基礎 字串
字串比較,stra.compareto strb a大於b 正數 a小於b 負數 a等於b 0 string stra ab string strb jk int intresult stra.compareto strb console.writeline intresult 查詢字串的位置 in...
C 字串的使用
一 標記 標記 tokenizing 是從文字中提取具體內容的過程。下面的 從句子中提取單詞,並把它們輸出到控制台。class mytokenizing int startpos 0 int endpos 0 dowhile startpos 二 顛倒字串次序 class myreverse 任何繼...