本文目錄一覽:
- 1、java中怎麼判斷一個字元串數組中包含某個字元或字元串
- 2、java怎麼判斷字元串中包含另一個字元串
- 3、Java查找字元串中是否包含
- 4、java怎麼查找字元串中是否包含某個欄位
- 5、java中怎麼判斷一個字元串中包含某個字元或字元串
- 6、java怎樣判斷一個字元串中的某個字元或字元串包含於另一個字元串
java中怎麼判斷一個字元串數組中包含某個字元或字元串
package test;
public class Test {
public static void main(String[] args) {
String str = “ab”;
System.out.println(isStr(str).toString());
}
/**
* 判斷一個字元串數組中包含某個字元或字元串:返回一個boolean:參數判斷的字元
* 1.定義一個字元串數組
* 2.遍歷這個數組
* 3.判斷要指定的字元串是否包含在字元串數組內
* 4.如果包含返回true,否則返回false
*/
public static Boolean isStr(String str){
String array[] = {“a”,”b”,”c”,”hello”};
for(int i = 0;iarray.length;i++){
if(str.equals(array[i])){
return true;
}
}
return false;
}
}
java怎麼判斷字元串中包含另一個字元串
String類中有一個方法 public boolean contains(Sting s)就是用來判斷當前字元串是否含有參數指定的字元串
例
s1=「takecatb」
s2=「te」
語句:s1.contains(s2) //s1調用這個方法
若其值為ture說明s1包含s2 若為fasle 則不包含
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怎麼查找字元串中是否包含某個欄位
方法:
1、描述:java.lang.String.contains() 方法返回true,當且僅當此字元串包含指定的char值序列
2、聲明:如下圖
3、返回值:此方法返回true,如果此字元串包含,否則返回false。
4、實例:如下圖
Java 基礎語法
一個Java程序可以認為是一系列對象的集合,而這些對象通過調用彼此的方法來協同工作。下面簡要介紹下類、對象、方法和實例變數的概念。
對象:對象是類的一個實例,有狀態和行為。例如,一條狗是一個對象,它的狀態有:顏色、名字、品種;行為有:搖尾巴、叫、吃等。
類:類是一個模板,它描述一類對象的行為和狀態。
方法:方法就是行為,一個類可以有很多方法。邏輯運算、數據修改以及所有動作都是在方法中完成的。
實例變數:每個對象都有獨特的實例變數,對象的狀態由這些實例變數的值決定。
java中怎麼判斷一個字元串中包含某個字元或字元串
//這段代碼可以判斷輸入字元串中包含某字元,並出現了多少次
public static void main(String[] args) throws IOException {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“輸入字元串:”);
String str1 = input.readLine();
System.out.print(“輸入字元:”);
String str2 = input.readLine();
char ch = str2.charAt(0);
System.out.println(“字元是”+ch);
int count=0;
for(int i=0;istr1.length();i++) {
if(ch==str1.charAt(i)){
count++;
}
}
System.out.println(“字元”+ch+”出現了”+count+”次”);
}
}
//如果你只要是否包含的判斷
public static void main(String[] args)
{
String str=”aab,asds,aa,ab,ba,baba,abbbba”;
if(str.indexOf(“ba”)!=-1){
System.out.println(“包含”);
}else{
System.out.println(“不包含”);
}
}
另外:Java中字元串中子串的查找共有四種方法,如下:
1、int indexOf(String str) :返回第一次出現的指定子字元串在此字元串中的索引。
2、int indexOf(String str, int startIndex):從指定的索引處開始,返回第一次出現的指定子字元串在此字元串中的索引。
3、int lastIndexOf(String str) :返回在此字元串中最右邊出現的指定子字元串的索引。
4、int lastIndexOf(String str, int startIndex) :從指定的索引處開始向後搜索,返回在此字元串中最後一次出現的指定子字元串的索引。
java怎樣判斷一個字元串中的某個字元或字元串包含於另一個字元串
假設你說的第一個字元串是A,第二個是B
判斷A中是否有一個字元或者一段字元串包含於B中:
boolean ifContrain = false;
for(int i = 0 ; i A.length – 1 ; i ++ )
{
for(int j = i + 1 ; j A.length ; j++ )
{
if(B.contains(A.subString(i , j )))
{
ifContrain = true;
}
}
}
最後看ifContrain是true,則包含,是false,就是不包含。
如果想要看包含的是哪段,就在ifContrain = true;一句後面再加一句 輸出 A.subString(i , j ) 就行了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/194647.html