「java字符串中查找字符」java怎么搜索文本框的字符串

博主:adminadmin 2022-12-27 14:57:11 58

本篇文章给大家谈谈java字符串中查找字符,以及java怎么搜索文本框的字符串对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 字符串中找字符串

str2 在str1中出现的位置和str3中位置问题。

假设:str1=abcabc str2=abc

第一次num1=0,str3=bcabc

第二次num1=2, str3 = str1.substring(++num2); 从str1第3位置截取:str3=abc

第三次num1=0;str3=从str1第1位置截取,str3=bcabc

。。。。

最终count=str1的长度。

看看下面代码:

import java.io.*;

class K6_4{

public static void main(String[] args)throws IOException{

BufferedReader br =

new BufferedReader(new InputStreamReader(System.in));

System.out.println("Input str1:");

String str1 = br.readLine();

System.out.println("Input str2:");

String str2 = br.readLine();

int count=0;

System.out.println(str1.substring(1));

while(true){

int index=str1.indexOf(str2);

if(index==-1)

break;

else {

count++;

str1=str1.substring(index+1);

}

}

System.out.println(count);

}

}

有问题就追问,满意请采纳!

java 如何查找匹配的字符和字符串

通过indexOf进行查找

示例:

String str = "abcdefg";

if(str.indexOf("cd")=0){//这里查找str中是否存在"cd"字符串,如果存在则会返回大于等于0的数,如果不存在,则返回-1

   System.out.println("找到了");

}

补充indexOf

1、返回 String 对象内第一次出现子字符串的字符位置。

2、string.indexOf(subString[, startIndex])

1)参数 

string

必选项。String 对象或文字。

subString 必选项。

要在 String 对象中查找的子字符串。

starIndex 可选项。

该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。

2)说明 

indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。

Java实现在字符串中查找字符串

String ss = "...qqqq:hello-a;hello-b;hello-c;hello-d,....";

Matcher matcher = Pattern.compile("(hello-[a-z])").matcher(ss);

int index = 0;

while (matcher.find()) {

index++;

if (matcher.group(1).equals("hello-c")) {

break;

}

}

System.out.println(index);

JAVA中怎样在一个字符串中查找给定的子字符串

可以用正则表达式:

import java.util.regex.Matcher;

import java.util.regex.Pattern;

class DemoAsm{

public static void main(String[] args){

String str="abcdef";//输入字符串

String reg="cde";//需要查找的字符串

Pattern pattern =Pattern.compile(reg);

Matcher mather=pattern.matcher(str);

while(mather.find()){

System.out.println(mather.group());//如果有,则输出

}

}

}

java如何实现在一个字符串中查找另一个字符串

要判断boy是不是后者中的一部分,不用循环,只要用String类的indexOf函数就行了。

代码如下:

public

class

HH

{

public

static

void

main(String[]

args)

{

String

s="he

is

a

boy";

int

result=s.indexOf("boy");

if(result=0){

System.out.println("boy是he

is

a

boy的一部分");

}else{

System.out.println("boy不是he

is

a

boy的一部分");

}

}

}

运行结果:

boy是he

is

a

boy的一部分

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字符串中查找字符的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java怎么搜索文本框的字符串、java字符串中查找字符的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。