當前位置:首頁 » 編程語言 » 字元串查找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);
}

}

熱點內容
增量調制編解碼實驗報告 發布:2025-02-01 14:30:30 瀏覽:787
不良人2無敵傷害腳本 發布:2025-02-01 14:23:04 瀏覽:396
地圖flash源碼 發布:2025-02-01 14:13:33 瀏覽:957
家庭影院配置什麼樣的音響 發布:2025-02-01 14:04:33 瀏覽:545
蘋果手機存儲空間不能用怎麼回事 發布:2025-02-01 14:03:04 瀏覽:259
qq易語言盜號源碼 發布:2025-02-01 14:01:25 瀏覽:812
源神比較好的雲伺服器 發布:2025-02-01 13:55:27 瀏覽:208
黑蘋果idea編譯慢 發布:2025-02-01 13:45:30 瀏覽:551
c和linux 發布:2025-02-01 13:39:38 瀏覽:177
android實現列表 發布:2025-02-01 13:38:06 瀏覽:103