当前位置:首页 » 编程语言 » 字符串查找java

字符串查找java

发布时间: 2022-07-17 22:50:10

java匹配字符串查找

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegrexTest {

public static void main(String[] args) {
Pattern p=Pattern.compile(".jsp");
Matcher m=p.matcher(".jsp||.jsp||.asp||.php||.aspx");
int i=0;
while(m.find()){
i++;
}
System.out.println(i);
}
}
看下符不符合要求,我简单试了一下能行,我没时间去考虑什么效率有没有漏洞,你拿去改改吧

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

调用类java.lang.String
的以下方法都可以:

public int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
参数:
str - 任意字符串。
返回:
如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。

public int indexOf(String str,int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
参数:
str - 要搜索的子字符串。
fromIndex - 开始搜索的索引位置。
返回:
指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

public int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。将最右边的空字符串 "" 视为出现在索引值 this.length() 处。
参数:
str - 要搜索的子字符串。
返回:
如果字符串参数作为一个子字符串在此对象中出现一次或多次,则返回最后一个这种子字符串的第一个字符。如果它不作为一个子字符串出现,则返回 -1。

public int lastIndexOf(String str,int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
参数:
str - 要搜索的子字符串。
fromIndex - 开始搜索的索引位置。
返回:
指定子字符串在此字符串中最后一次出现处的索引。

❸ java 查找某字符串

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test {
public static void main(String[] args) throws IOException{
//目标字符串
String target = "20000";
//注意修改文件路径
BufferedReader br = new BufferedReader(new FileReader("D:/ks.txt"));
String line = null;
while(null != (line = br.readLine())){
if(line.contains(target)){
//截取最后一个分号以后的部分,并作为结果输出
System.out.println("结果为:"+ line.substring(line.lastIndexOf(";") + 1));
break;
}
}
br.close();
}
}

❹ 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实现在字符串中查找字符串

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中如何查找字符串中的'\'

可以用正则表达式.
如果要表示java源码中的正则表达式的一个正常的'\'字符,则需要这样表示'\\\\',其中第一第三个'\'均表示java编译器中的转义字符第二个则表示正则表达式中的转义字符,从而把第四个'\'转义为正则表达式中一个普通的'\'字符

❼ 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 如何查找匹配的字符和字符串

你可以自己写个方法的!
从返回的第一个位置开始substring,同时记住位置。

public int[] getOffset(String str,String s){
int[] arr=new int[str.length];
int j=1;
while(str.indexOf(s)!=-1){
int i=str.indexOf(s);
if(j==1){
arr[j-1]=i;
}else{
arr[j-1]=i+arr[j-2]+1;
}
String st=str.substring(i+1);
System.out.println(st);
str=st;
j++;
System.out.println("j="+j);
}
return arr;
}

public static void main(String[] args) {

String str="abcaabbddab";
StringText st=new StringText();
int[] i=st.getOffset(str, "ab");
for(int j:i){
System.out.println(j);
}
}

❾ JAVA字符串检索

返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始,这样检索效率更高点。这种问题看看api就是了

❿ 简单的java字符串中查找字符问题

代码如下:

package demo.joshua;

public class MyFile {

public static void main(String[] args) {
// TODO Auto-generated method stub
filterStr("我喜欢Java程序设计");
filterStr("汉语(Chinese)是我们的母语");
}

public static void filterStr(String str) {
Integer sum = 0;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < 171948 && str.charAt(i) > 19968) {
sb.append(str.charAt(i));
sum++;
}
}
System.out.println("字符串中包含的汉字:" + sb.toString());
System.out.println("汉字的个数:" + sum);
}

}

热点内容
绝对赛车3安卓在哪里下载 发布:2025-02-01 12:42:30 浏览:715
mysql修改数据库字符 发布:2025-02-01 12:37:52 浏览:567
阿里云服务器出厂密码是多少 发布:2025-02-01 12:35:43 浏览:665
手机传文件服务器和ip地址 发布:2025-02-01 12:15:01 浏览:657
儿子编程课 发布:2025-02-01 12:15:00 浏览:900
zsh脚本 发布:2025-02-01 12:13:48 浏览:595
android亮度获取 发布:2025-02-01 12:09:10 浏览:624
小孩什么时候学编程比较好 发布:2025-02-01 12:03:10 浏览:960
c语言的认识 发布:2025-02-01 11:58:03 浏览:520
svn连接服务器地址 发布:2025-02-01 11:51:31 浏览:416