java查找字元串的字元串
㈠ 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怎麼查找字元串第一個字元
通過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中如何查找字元串
問題很簡單:
1.首先,你的數據源是數組,那麼要想對每一個值作操作,必須遍歷,所以就有如下代碼:
for(int i=0;i<A.length;i++){
...
}
2.在循環當中,數組中的每一個正在遍歷的元素,就是:A[i];
3.String是java的一個字元串處理類,他有一個非常好用的方法就是,
public boolean startsWith(String prefix),測試此字元串是否以指定的前綴開始。 所以:A[i].startsWith("C")如果返回true,那麼他就是以C開頭的。
4.綜上所述,實現很簡單,完成代碼如下:
public class Test{
public static void main(String[] args){
String[] A ={"CSDF","FDS","CFDSA","DFS","FET"};
for(int i=0;i<A.length;i++){
if(A[i].startsWith("C")){
System.out.println(A[i]);
}
}
}
}
總結:
臨時寫的,代碼沒有經過測試,但敢保證其正確性的幾率很大,祝你成功。
㈣ 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中如何查找字元串中的'\'
可以用正則表達式.
如果要表示java源碼中的正則表達式的一個正常的'\'字元,則需要這樣表示'\\\\',其中第一第三個'\'均表示java編譯器中的轉義字元第二個則表示正則表達式中的轉義字元,從而把第四個'\'轉義為正則表達式中一個普通的'\'字元
㈥ java中怎麼實現在一個字元串中查找其中的關鍵字。
public class $ {
public static void main(String... _) {
String str = "123456789 abcdefg hijklmn...";
System.out.println(str.indexOf("456"));
System.out.println(str.indexOf("45a"));
}
}
結果:
3
-1
如果有,就返回他的起始位置,注意是從0開始
沒有,就返回-1
用循環
String[] key = { "456", "abc", "45a" };
String str = "123456789 abcdefg hijklmn...";
for (int i = 0; i < key.length; i++) {
System.out.println(key[i] + "的起始位置:" + str.indexOf(key[i]));
}
㈦ 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實現在字元串中查找字元串
把上抄上面那仁兄襲的改改,不知合不合適:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Find {
public static void main(String[] args) {
String ss = "...qqqq:hello-a;hello-b;hello-c;hello-d,....";
Matcher matcher = Pattern.compile("(hello-.)").matcher(ss);
int index = 0;
while (matcher.find()) {
index++;
if (matcher.group(1).equals("hello-c")) {
break;
}
}
System.out.println(index);
}
}
㈩ java中怎麼判斷一個字元串中包含某個字元或字元串
string
型的字元串一旦確定就無法對其進行改變,這個時候你可調用各個方法來實現
比如
indexof(x):返回字元串中出現的第一個x的下標,如果沒有匹配的返回-1
contains(s1):如果s1是該字元串的子串,返回true。
還有很多都是string的操作,如果是字元數組,只需循環一下下標,挨個比較就好。