1. java中如何高效獲得字元串中特定字元的所有位置
你試下下面的代碼能滿意嗎 ,我已經試過了,可以的:
public static void main(String args[]) {
String str = "12&32&位置&yutye";
System.out.println("&在字元串中出現的位置分別為:");
for(int i=-1; i<=str.lastIndexOf("&");++i)
{
i=str.indexOf("&",i);
System.out.print(i+"\t");
}
}
2. java中如何能查詢出字元串中某個字母的位置
str為你要測試的字元串第一種方法:byte[]temp=str.getbytes();//使用平台默認的字元集將此string解碼為位元組序列,並將結果存儲到一個新的位元組數組中。intcount=0;//遍歷數組的每一個元素,也就是字元串中的每一個字母for(inti=0;i<temp.length;i++){//如果字母等於cif(temp[i].equals('c')){//計數器加一count++;}}第二種:intcount=0;stringstr=//你要測試的字元串//index為字元串中第一次出現c的位置,如果字元串中沒有c將返回-1intindex=str.indexof(c);//如果字元串中有cwhile(str.indexof(c)!=-1){count++;//將字元串出現c的位置之前的全部截取掉str=str.substring(str.indexof(c));}考慮大小寫:str=str.tolowercase();//將字元串全部轉化成小寫
3. 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第一次出現的位置的下一位開始再找出匹配的字元串。
4. java怎樣獲取特殊字元的位置
1
2
3
4
String str="abcdef";
// indexOf方法可以獲取指定字元串w在主串S的下標
// 下標由0開始,所以此處查找字元串c在主串下標為2的位置。
System.out.println(str.indexOf("c")); // print 2