導航:首頁 > 編程語言 > java判斷包含字元串

java判斷包含字元串

發布時間:2023-02-02 03:03:19

java中怎麼判斷一個字元串中包含某個字元或字元串

public static void main(String[] args) {
String str="ABC_001";
if(str.indexOf("ABC")!=-1){
System.out.println("包含");
}else{ System.out.println("不包含");
}
}

js 判斷字元串是否包含某字元串,String對象中查找子字元,indexOf
var Cts
= "bblText";

if(Cts.indexOf("Text")
> 0 )
{
alert('Cts中包含Text字元串');
}
indexOf用法:

返回 String 對象內第一次出現子字元串的字元位置。

strObj.indexOf(subString[, startIndex])

參數
strObj

必選項。String 對象或文字。

subString

必選項。要在 String 對象中查找的子字元串。

starIndex

可選項。該整數值指出在 String 對象內開始查找的索引。如果省略,則從字元串的開始處查找。

說明
indexOf 方法返回一個整數值,指出 String 對象內子字元串的開始位置。如果沒有找到子字元串,則返回 -1。

如果 startindex 是負數,則 startindex 被當作零。如果它比最大的字元位置索引還大,則它被當作最大的可能索引。

從左向右執行查找。否則,該方法與 lastIndexOf 相同。

示例
下面的示例說明了 indexOf 方法的用法。

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}

對於JavaScript的indexOf忽略大小寫

JavaScript中indexOf函數方法返回一個整數值,指出 String 對象內子字元串的開始位置。如果沒有找到子字元串,則返回 -1。如果 startindex 是負數,則 startindex 被當作零。如果它比最大的字元位置索引還大,則它被當作最大的可能索引。

indexOf函數是從左向右執行查找。否則,該方法與 lastIndexOf 相同。

下面的示例說明了indexOf函數方法的用法。

function IndexDemo(str2){
var str1
= "BABEBIBOBUBABEBIBOBU"
var s
= str1.indexOf(str2);
return(s);
}

㈡ java 中怎樣判斷是否 包含某個字元串

publicstaticvoidmain(String[]args){
Stringstr="ABC_001";
if(str.indexOf("ABC")!=-1){
System.out.println("包含");
}else{System.out.println("不包含");
}
}

java截取相關

1、length() 字元串的長度
例:char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();

2、charAt() 截取一個字元
例:char ch;
ch="abc".charAt(1); 返回'b'

3、getChars() 截取多個字元
void getChars(int sourceStart,intsourceEnd,char target[],int targetStart)
sourceStart指定了子串開始字元的下標,sourceEnd指定了子串結束後的下一個字元的下標。因此,子串包含從sourceStart到sourceEnd-1的字元。接收字元的數組由target指定,target中開始復制子串的下標值是targetStart。

例:String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);

4、getBytes()
替代getChars()的一種方法是將字元存儲在位元組數組中,該方法即getBytes()。

5、toCharArray()

6、equals()和equalsIgnoreCase() 比較兩個字元串

7、regionMatches() 用於比較一個字元串中特定區域與另一特定區域,它有一個重載的形式允許在比較中忽略大小寫。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)

8、startsWith()和endsWith()
startsWith()方法決定是否以特定字元串開始,endWith()方法決定是否以特定字元串結束

9、equals()和==
equals()方法比較字元串對象中的字元,==運算符比較兩個對象是否引用同一實例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false

10、compareTo()和compareToIgnoreCase() 比較字元串

11、indexOf()和lastIndexOf()
indexOf() 查找字元或者子串第一次出現的地方。
lastIndexOf() 查找字元或者子串是後一次出現的地方。

12、substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)

13、concat() 連接兩個字元串

14 、replace() 替換
它有兩種形式,第一種形式用一個字元在調用字元串中所有出現某個字元的地方進行替換,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');
第二種形式是用一個字元序列替換另一個字元序列,形式如下:
String replace(CharSequence original,CharSequence replacement)

15、trim() 去掉起始和結尾的空格

16、valueOf() 轉換為字元串

17、toLowerCase() 轉換為小寫

18、toUpperCase() 轉換為大寫

19、StringBuffer構造函數
StringBuffer定義了三個構造函數:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

(1)、length()和capacity()
一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。

(2)、ensureCapacity() 設置緩沖區的大小
void ensureCapacity(int capacity)

(3)、setLength() 設置緩沖區的長度
void setLength(int len)

(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)

(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

(6)、append() 可把任何類型數據的字元串表示連接到調用的StringBuffer對象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();

(7)、insert() 插入字元串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定將字元串插入到StringBuffer對象中的位置的下標。

(8)、reverse() 顛倒StringBuffer對象中的字元
StringBuffer reverse()

(9)、delete()和deleteCharAt() 刪除字元
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)

(10)、replace() 替換
StringBuffer replace(int startIndex,int endIndex,String str)

(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)

㈢ java 怎麼判斷一個字元串是否包含另一個字元串

s1.contains(s2) 方法可以

packagecom.yii;

importjava.lang.*;

publicclassStringDemo{

publicstaticvoidmain(String[]args){

Stringstr1="tutorialspoint",str2="http://";

CharSequencecs1="int";

//
booleanretval=str1.contains(cs1);
System.out.println("Methodreturns:"+retval);

//
retval=str2.contains("_");
System.out.println("Methodsreturns:"+retval);
}
}

輸出結果:
Methodreturns:true(包含)
Methodsreturns:false(不包含)

㈣ java怎麼判斷字元串中包含另一個字元串

String類中有一個方法 public boolean contains(Sting s)就是用來判斷當前字元串是否含有參數指定的字元串

s1=「takecatb」
s2=「te」
語句:s1.contains(s2) //s1調用這個方法
若其值為ture說明s1包含s2 若為fasle 則不包含

㈤ java中怎麼判斷一個字元串數組中包含某個字元或字元串

/*這是一個靜態函數,不用聲明對象就可以用的,如你的類名為Test,在任何情況下都可以調用Test.isHave函數*/
public static boolean isHave(String[] strs,String s){
/*此方法有兩個參數,第一個是要查找的字元串數組,第二個是要查找的字元或字元串
* */
for(int i=0;i<strs.length;i++){
if(strs[i].indexOf(s)!=-1){//循環查找字元串數組中的每個字元串中是否包含所有查找的內容
return true;//查找到了就返回真,不在繼續查詢
}
}
return false;//沒找到返回false
}
public static void main(String[] args)
{

String[] strs={"aaa","bbbb","cccc","dddd"};//定義字元串數組

if(isHave(strs,"aaaa")){//調用自己定義的函數isHave,如果包含則返回true,否則返回false
System.out.println("包含");//列印結果
}else{
System.out.println("不包含");//列印結果
}
}

㈥ java中怎麼判斷一個字元串中包含某個字元或字元串

/*這是一個靜態函數,不用聲明對象就可以用的,如你的類名為Test,在任何情況下都可以調用Test.isHave函數*/
public static boolean isHave(String[] strs,String s){
/*此方法有兩個參數,第一個是要查找的字元串數組,第二個是要查找的字元或字元串
* */
for(int i=0;i<strs.length;i++){
if(strs[i].indexOf(s)!=-1){//循環查找字元串數組中的每個字元串中是否包含所有查找的內容
return true;//查找到了就返回真,不在繼續查詢
}
}
return false;//沒找到返回false
}
public static void main(String[] args)
{

String[] strs={"aaa","bbbb","cccc","dddd"};//定義字元串數組

if(isHave(strs,"aaaa")){//調用自己定義的函數isHave,如果包含則返回true,否則返回false
System.out.println("包含");//列印結果
}else{
System.out.println("不包含");//列印結果
}
}
或者用另外一個方法
indexOf方法,例如:
String a="abc";
int i=a.indexOf("b");
i就是得到a裡面b字元的索引,如果i大於-1則表示a中有b字元.

string1.contains(string2),若為true則表示包含,這個是區分大小寫的,假如你想無區分的話,string1.toLowCase().contains(string2.toLowCase().),先轉小寫字元串再判斷.
方法比較多,就看您具體是什麼情況了,如有不懂可以問問ITJOB工程師。

㈦ java中怎麼判斷一個字元串中包含某個字元或字元串

方法:

使用String類的indexOf()方法可以判斷一個字元串是否在另一個字元串中出現,其方法原型為:

intjava.lang.String.indexOf(Stringarg0)


如果字元串arg0出現在源字元串中,返回arg0在源字元串中首次出現的位置。


Java程序

publicclassMain{
publicstaticvoidmain(String[]args){
Stringkey="wo";
char[]arr={'H','e','l','l','o',',','','w','o','r','l','d','!'};
Stringsource=String.valueOf(arr);

if(source.indexOf(key)>=0){
System.out.printf(""%s"中包含"%s"",source,key);
}
else{
System.out.printf(""%s"中不包含"%s"",source,key);
}
}
}


運行測試:

"Hello,world!"中包含"wo"

㈧ java中怎麼判斷一個字元串中包含某個字元或字元串

//這段代碼可以判斷輸入字元串中包含某字元,並出現了多少次
publicstaticvoidmain(String[]args)throwsIOException{
BufferedReaderinput=newBufferedReader(newInputStreamReader(System.in));
System.out.println("輸入字元串:");
Stringstr1=input.readLine();
System.out.print("輸入字元:");
Stringstr2=input.readLine();
charch=str2.charAt(0);
System.out.println("字元是"+ch);
intcount=0;
for(inti=0;i<str1.length();i++){
if(ch==str1.charAt(i)){
count++;
}
}
System.out.println("字元"+ch+"出現了"+count+"次");
}
}
//如果你只要是否包含的判斷
publicstaticvoidmain(String[]args)
{

Stringstr="aab,asds,aa,ab,ba,baba,abbbba";
if(str.indexOf("ba")!=-1){
System.out.println("包含");
}else{
System.out.println("不包含");
}
}另外:Java中字元串中子串的查找共有四種方法,如下:
1、intindexOf(Stringstr):返回第一次出現的指定子字元串在此字元串中的索引。
2、intindexOf(Stringstr,intstartIndex):從指定的索引處開始,返回第一次出現的指定子字元串在此字元串中的索引。
3、intlastIndexOf(Stringstr):返回在此字元串中最右邊出現的指定子字元串的索引。
4、intlastIndexOf(Stringstr,intstartIndex):從指定的索引處開始向後搜索,返回在此字元串中最後一次出現的指定子字元串的索引。

㈨ 判斷java或js中的某個字元串中是否包含有某個字元或是字元串

在java中一般有兩種方法較常用,分別是contains(String str)和indexOf(String str)。

其中contains返回值為boolean類型,true為有,false為沒有;而indexOf實際上是查找一個字元串在另一個字元串的位置的一個方法,且以匹配好的第一個字元為准;所以該方法的返回值為int類型,其中 -1表示未找到,其餘都是能找到意思。所以一般來講,java中的判斷方式如下:

Stringstr="abcde";
//第一種方法
if(str.contains("b")){
System.out.println("yes");
}else{
System.out.println("no");
}

//第二種方法
if(str.indexOf("bc")>=0){
System.out.println(str.indexOf("bc"));
System.out.println("yes");
}else{
System.out.println("no");
}

而在js中較為常見方法為indexOf(),返回值同java一樣,為最常用的方法;隨後,ES6又提供了三種新方法。includes(),返回布爾值,表示是否找到了參數字元串;startsWith(),返回布爾值,表示參數字元串是否在源字元串的頭部;endsWith(),返回布爾值,表示參數字元串是否在源字元串的尾部。

vars='Helloworld!';

if(s.indexOf('world')>=0){
console.log('true');
}
if(s.includes('o')){
console.log('true');
}
if(s.startsWith('Hello')){
console.log('true');
}
if(s.endsWith('!')){
console.log('true');
}
閱讀全文

與java判斷包含字元串相關的資料

熱點內容
智行app鉑金會員怎麼還有期限 瀏覽:581
win10用子文件夾改名 瀏覽:234
ae鋼筆工具在哪裡 瀏覽:460
gn105數據線插哪裡 瀏覽:916
破鎖屏密碼方法 瀏覽:835
股票數據放哪裡 瀏覽:576
m格式庫文件 瀏覽:279
天際通數據服務怎麼開票 瀏覽:430
寫小說發哪個網站比較好 瀏覽:244
小米電視3藍牙文件路徑 瀏覽:111
shell讀取文件值 瀏覽:909
文件夾路徑欄消失 瀏覽:795
律師哪些業務不能代替大數據 瀏覽:952
lol哪些文件可以刪除 瀏覽:701
匯編程序中del是什麼意思 瀏覽:183
幼兒園免費網站模板下載 瀏覽:210
w619線刷教程 瀏覽:759
怎麼培養編程思想 瀏覽:697
手機捆綁app的軟體怎麼卸載 瀏覽:32
vb編程器有什麼用 瀏覽:999

友情鏈接