『壹』 java查找字元串中是否包含

import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
/**
* @author:
* @description:
*/
public class Test {
static int count = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入字元串:");
String line1 = scanner.nextLine();
System.out.println("請輸入包含的字元串:");
String line2 = scanner.nextLine();
int count = sort(line2, line1);
System.out.println("首次出現的位置:"+(line1.indexOf(line2)+1)+","+"共出現:"+count+"次");
}
public static int sort(String t, String s) {

int indexOf = s.indexOf(t);
if(indexOf==-1){
System.out.println("未找到指定的字元串!");
}else{
count++;
String string = s.substring(indexOf+1,s.length());
if(!StringUtils.isBlank(string)){
sort(t, string);
}
}
return count;

}
}

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

方法如下:

一、contains方法

1:描述

java.lang.String.contains() 方法返回true,當且僅當此字元串包含指定的char值序列

2:聲明

publicbooleancontains(CharSequences)

3:返回值

此方法返回true,如果此字元串包含,否則返回false。

4:實例

publicstaticvoidmain(String[]args){
Stringstr="abc";
booleanstatus=str.contains("a");
if(status){
System.out.println("包含");
}else{
System.out.println("不包含");
}
}

二、indexOf方法

1:描述

java.lang.String.indexOf() 的用途是在一個字元串中尋找一個字的位置,同時也可以判斷一個字元串中是否包含某個字元。

2:聲明

intindexOf(intch,intfromIndex)

3:返回值

indexOf的返回值為int

4:實例

publicstaticvoidmain(String[]args){
Stringstr1="abcdefg";
intresult1=str1.indexOf("a");
if(result1!=-1){
System.out.println("字元串str中包含子串「a」"+result1);
}else{
System.out.println("字元串str中不包含子串「a」"+result1);
}
}

『叄』 java 字元串包含某個字元有多少個

工具:

MyEclipse
方法:

1、打開MyEclipse 並新建類「Test」

2、編寫判斷版方法,其實方法很權簡單只有兩行代碼

1
2
3

public static boolean isContain(String s1,String s2) {
return s1.contains(s2);
}

第一個參數s1 中是否包含s2

3、編寫main方法代碼:

1
2
3
4

public static void main(String[] args) {
Test t = new Test();
System.out.println(t.isContain("123", "3"));

『肆』 java中怎麼判斷字元串中包含字元個數

/*
*Email:[email protected]
*/
publicintcountChar(Stringstr,charch){
//將字元串轉換為字元數組
char[]chs=str.toCharArray();
//定義變數count存儲字元串出現次數
intcount=0;
for(inti=0;i<chs.length;i++){
if(chs[i]==ch){
count++;
}
}
returncount;
}

如果要知道字元個數,也就是字元串長度。直接調用length()方法即可。

『伍』 java「包含」字元的正則表達式

Pattern.matches("^[a-z]+$",s)
Pattern.matches(".*a+.*",s)&&Pattern.matches(".*b+.*",s)

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

string
型的字元串一旦確定就無法對其進行改變,這個時候你可調用各個方法來實現
比如
indexof(x):返回字元串中出現的第一個x的下標,如果沒有匹配的返回-1
contains(s1):如果s1是該字元串的子串,返回true。
還有很多都是string的操作,如果是字元數組,只需循環一下下標,挨個比較就好。

『柒』 java怎樣判斷一個字元串中的某個字元或字元串包含於另一個字元串

完全可以利用java系統給的函數indexof(),來判斷一個字元中是否包含回另一個字元串答:
列如:
String i = 「songfeilong」;
if(i.indexos("s")>0){
System.out.println("包含s字元串");
}
else{
System.out.println("不包含s字元串");

}
indexof()函數返回的是一個整數

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

str.indexOf("xxx")
如過沒有返回-1 有的話會返回出現該字元的第一個位置(位置是從0開始數的)

『玖』 java判斷輸入的字元串中是否包含某個字元

用contains(CharSequence s)方法,比如:
strA.contains(strB)

測試 字元串 strA 中是否包含字元串 strB
如果是字元的話,可以用Character.toString(char c)方法來把它轉換成字元串。
比如:
String s1="abcdef";
char ch='c';
System.out.println(s1.contains(Character.toString(ch)));
輸出結果:true

『拾』 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);
}