c 中字串的常用函式

2021-06-09 23:53:20 字數 1727 閱讀 1290

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace str

str1 = str1.tolower();//返回string型別,把字串變為小寫

console.writeline(str1);

str1 = str1.toupper();//返回string型別,把字串變為大寫

console.writeline(str1);

bool b = str1.equals("abc", stringcomparison.ordinalignorecase);//忽略大小寫比較

console.writeline(b);

string str2 = "    what a good day!     ";

str2 = str2.trim();//去掉字串兩邊的紅白,中間的空白不去除

console.writeline(str2);

string str3 = "aa,b|c,ff.h";

string str4 = str3.split(',', '|', '.');

foreach(string item1 in str4)

string str5 = ",,c,,b,c,,,,c";

string str6 = str5.split(new char , stringsplitoptions.removeemptyentries);

foreach (string item2 in str6)

string str7 = "我是狗我是貓我是王八蛋";

string str8 = str7.split(new string , stringsplitoptions.removeemptyentries);

foreach(string item3 in str8)

str7 = str7.replace("我是", "你是");//原型string replace(string oldstring, string newstring);//用新串替換舊竄

console.writeline(str7);

str7 = str7.substring(6);//得到從某個位置開始至字串末尾的子串

console.writeline(str7);

str7 = str7.substring(0, 2);//得到從某個位置開始並且長度為length的子串

console.writeline(str7);

string str9 = "what a good day! let's party,goodbye";

bool b1 = str9.contains("what");//判斷串中是否含有某個串

console.writeline(b1);

bool b2 = str9.startswith("where");//判斷串是否是以某個串開始

console.writeline(b2);

bool b3 = str9.endswith("bye");//判斷串是否是以某個串結束

console.writeline(b3);

int ipos = str9.indexof("day");//某個串第一次出現的位置

console.writeline(ipos);

console.readkey();}}

}

C 中字串分割的常用函式

經常碰到字串分割的問題,這裡總結下,也方便我以後使用。一 用strtok 函式進行字串分割 原型 char strtok char str,const char delim 功能 分解字串為一組字串。引數說明 str 為要分解的字串,delim 為分隔符字串。返回值 從s tr開頭開始的乙個個被分割...

實現C字串常用函式

字串長度 int stringlen const char str 字串比較 int stringcompare const char dst,const char src 字串複製 char stringcopy char dst,const char src 字串連線 char stringco...

c語言字串常用函式

1.strcat函式 字串連線函式。include stdio.h include string.h 為了引用strcat函式。intmain str2 printf s strcat str1,str2 strcat a字元陣列,b字元陣列 strcat函式 字串連線函式 a字元陣列必須足夠大,以...