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());
}
}