java字元串位置
❶ java獲取字元位置
給你三個方法。都能找出其位置並列印出結果
// 1.使用正則表達式方法
Pattern p = Pattern.compile(",");
String s1 = "abc,,de,f,g,hi,jkl";
Matcher m = p.matcher(s1);
// 將所有匹配的","的位置都列印出來,並統計次數
int cnt1 = 0;
while (m.find()) {
cnt1++;
System.out.println(m.start());
}
System.out.println("使用第1次方法檢測出的,出現次數: " + cnt1);
// 2.使用String自帶的indexOf方法
String s2 = s1;
int pos = 0;
int cnt2 = 0;
while (pos < s2.length()) {
pos = s2.indexOf(',', pos);
if (pos == -1) {
break;
} else {
cnt2++;
System.out.println(pos);
pos++;
}
}
System.out.println("使用第2次方法檢測出的,出現次數: " + cnt2);
// 3.取出字元串String的每個字元逐一比較
String s3 = s1;
int cnt3 = 0;
for (int i = 0; i < s3.length(); i++) {
if (s3.charAt(i) == ',') {
cnt3++;
System.out.println(i);
}
}
System.out.println("使用第3次方法檢測出的,出現次數: " + cnt3);
❷ java中怎麼輸出字元所在位置
用indexOf(' 你希望要的字元 '),這個函數得到字元串中第一次出現指定字元的下腳標版(腳標從0開始)
比如權:
String str = "abc";
System.out.println(str.indexOf(『a』));
輸出的就是0
❸ java 字元串 在另一個字元串中 出現的起始位置
您好,提問者:
N.indexOf(b);//正向搜索,起始位置
N.lastIndexOf(b);//反向搜索,最終位置
你的代碼是沒有錯的!請問還有什麼問題?
❹ 在java如何查找字元串位置
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Please input data:");
String data = br.readLine();
while(data==null||data.trim().length()==0){
data = br.readLine();
}
System.out.println("you data is :"+data);
System.out.println("Please input position:");
String[] p = br.readLine().split(",");
for (String string : p) {
System.out.println(string);
}
int[] postion = new int[data.length()];
int i=0;
for (String string : p) {
postion[i]= Integer.parseInt(p[i]);
i++;
}
System.out.println("this Result:");
for(int y=0;y<postion.length&&postion[y]>0;y++){
char c = data.charAt(postion[y]-1);
System.out.print(c);
}
❺ JAVA字元串中取特定字元的位置
int indexOf(int ch)
返回指定字元在此字元串中第一次出現處的索引。
int indexOf(int ch, int fromIndex)
返回在此字元串中第一次出現指定字元處的索引,從指定的索引開始搜索。
int indexOf(String str)
返回指定子字元串在此字元串中第一次出現處的索引。
int indexOf(String str, int fromIndex)
返回指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。
❻ java 怎樣轉換字元串中字元位置
「字元串數組」 轉 「字元串」,通過循環,沒有其它方法
String[] str = {"abc", "bcd", "def"};
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length; i++){
sb. append(str[i]);
}
String s = sb.toString();
「字元數組」內 轉 「字元串」 可以通過下邊容的方法
char[] data={'a','b','c'};
String s=new String(data);
❼ java字元串位置
提供一下思路,indexof()取得第一個後數值後a,用substring把字元串拆開,要後半部分,繼續indexof()取得數值b,位置就是a+b。以此類推遞歸下就可以!
❽ Java如何獲取一個字元串中子串的位置
//你描述的不是很清楚請問是一下這個意思嗎?
="ABCDEFGHIJKLMN";
publicstaticvoidmain(String[]args){
test("A");
}
publicstaticvoidtest(Stringa){
for(inti=0;i<CONVERT.length();i++){
//判斷當前字元串中的單個位置
if(a.equals(String.valueOf(CONVERT.charAt(i)))){
//當前字元串
System.out.println(CONVERT.charAt(i));
//獲取當前字元串位置
System.out.println(i+1);
}
}
}
❾ java如何獲取字元位置
Java中String提供的常用操作函數:char charAt(int index)。返回指定索引處的 char 值。
具體操作:
條件是:
(index = str1.indexOf(str2, index + 1)) >= 0 str1.indexOf(str2, index + 1) 查出str2從左到右第一次出現的位置, index = str1.indexOf(str2, index + 1) 將位置賦值給index變數。
下次循環開始時,因為str1.indexOf(str2, index + 1)第二個參數是index+1,所以從str2第一次出現的位置的下一位開始再找出匹配的字元串。
❿ java 查找字元串的位置
import java.util.Scanner;
public class FindIndex {
public static void main(String[] args) {
System.out.println("請輸入字元串:");
Scanner in=new Scanner(System.in);
String str=in.nextLine();
System.out.println("請輸入要查找的字元:");
char c=in.nextLine().charAt(0);
StringBuffer bf=new StringBuffer();
for(int i=0;i<str.length();i++){
if(str.charAt(i)==c){
bf.append(i+" ");
}
}
System.out.println(bf.toString());
}
}