c如何判斷字串中是否包含某個字元

2021-04-14 05:45:38 字數 2156 閱讀 5822

1樓:匿名使用者

using system;

using system.collections;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;}}

擴充套件資料:

c#字串處理

1、從字串中提取子串

stringbuilder 類沒有支援子串的方法,因此必須用string類來提取。(關於stringbuilder 類,文末會提到)

string mystring="my name is ynn.";

//displays "name is ynn."

console.writeline(mystring.substring( 3 ));

//displays "ynn"

console.writeline(mystring.substring( 11,3 ));

2、比較字串

string 類有四種方法:compare( )、compareto( )、compareordinal( )、equals( )。

compare( )方法是compareto( )方法的靜態版本。只要使用「=」運算子,就會呼叫equals( )方法,的以equals( )方法與「=」是等價的。compareordinal( )方法對兩個字串比較不考本地語言與檔案。

示例:int result;

bool bresult;

s1="aaaa";

s2="bbbb";

//compare( )method

//result值為「0」表示等,小於零表示 s1 < s2,大於零表示 s1 > s2

result=string.compare(s1,s2);

result=s1.compareto( s2 );

result=string.compareordinal(s1,s2)

2樓:匿名使用者

使用字串的indexof方法查詢,找到返回正確的位置下標,未找到版返回-1

舉例:string str = "我愛權北京天安門";

int index = str.indexof("愛");

if (index > -1)

else

console.read();

3樓:張向琳

你這裡用bai到的是陣列du 當然不能這樣寫啦 要用zhifor迴圈dao或者foreach遍歷出來版

一遍權一遍的查出來 你這樣當然會報錯啊. 像下面這樣寫string number = "0";

bool exists=false;

string str = ;

for (int i = 0; i < str.length; i++)

if (exists)

console.writeline(number);

我用是2010 不知道2005有沒有contains這個方法希望能幫助到你!

4樓:匿名使用者

首先,copy

如果過濾多個字串,那就要bai遍歷字串陣列。du然後再做判斷。

例如:for(int i=0;i處可以zhi做多個處理。例dao如替換掉匹配的部分字串,,組成新的字串,而不是整個清空。}}

5樓:匿名使用者

string s="我們的社會真和諧";

if(s.contains="s的一部分")

indexof(char型別)

如何判斷字串中是否包含某個字元

6樓:匿名使用者

比如查詢 字串 "abdbmdef" 裡是否包含'm'

string str = "abdmdef"

int pos = str.find("m")pos 會返回"m"的所在位置 , 如果沒有找到就會返回-1

7樓:育知同創教育

string  txt="asdjsfk/aaaaaaaaa";

if(txt.contains("/"))//檢驗「/」

js中如何判斷字串中是否包含指定字串

正則bai 匹配 du var str this is my test var test new regexp test g 建立正 zhi則dao表達版式對權 象var result s.match test alert result js判斷字串變數是否含有某個字串的方法 方法一 var str...

如何判斷字串是否相等,判斷字串是否有值,判斷多個字串是否相等

string str1 abc string str2 bcd if str1.equals str2 用的是專equals 方法屬 先要包含string.h 標頭檔案,再用以下 比較str1與str2二個字串是否相等 if strcmp str1,str2 0 在pl sql怎麼判斷兩個字串是否相...

js中怎麼判斷字串是否在另字串中

js中判斷一個字抄符串包含另外 襲一個字串的方式比較多,比如indexof 方法,注意o是大寫。var test this is a test if test.indexof test 1 es6又提供了三種新方法 1 includes 返回布林值,表示是否找到了引數字串。2 startswith ...