導航:首頁 > 編程語言 > java字元出現位置

java字元出現位置

發布時間:2024-12-05 03:50:21

① 如何用java求一個字元串在另一個字元串中出現的位置

import java.util.*;

public class CountChars {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Please Input Your String!");
String str = sc.nextLine();
Map<Character, Integer> map = countLetters(str);
System.out.println("total kinds: " + map.size());

for (Map.Entry<Character, Integer> entry : map.entrySet()) { //增強的for循環
System.out.printf("letter %c: %d\n", entry.getKey(), entry.getValue());
}
}

static Map<Character, Integer> countLetters(String s) {
if (s == null) {
return null;
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
char c;
Integer oldValue;
int newValue;
for (int i = 0; i < s.length(); ++i) {
c = s.charAt(i);
oldValue = map.get(c);
newValue = (oldValue == null) ? 1 : oldValue.intValue() + 1;
map.put(c, newValue);
}
return map;
}
}

② java 字元串 在另一個字元串中 出現的起始位置

可以通過indexOf方法進行位置判斷。舉例:

int length = "abcdbe".indexOf("bc")+1;//因為是從開始計數,所以加1

int end = length+"bc".length()-1;//取得結束位置

System.out.print("開始位置:"+length+", 結束位置:"+end);

輸出結果:

③ java中如何顯示字元串位置

java中輸出字元所在位置可以使用indexOf()函數
例子:
System.out.println("abcd".indexOf("b"));
結果輸出1
返回b字元在字元串abcd中第一次出現的位置

④ java 怎麼獲得字元串中某一字元的位置

在java中使用indexOf方法即可獲得字元串中某一字元的位置,例如Stringstr="abcdef",System.out.println(str.indexOf("c"))。

⑤ 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();//將字元串全部轉化成小寫

閱讀全文

與java字元出現位置相關的資料

熱點內容
人民幣怎麼演算法 瀏覽:754
什麼app可以聽懂刺蝟說話 瀏覽:596
安卓機內存小如何擴大 瀏覽:125
粉絲伺服器怎麼和安卓手機通信 瀏覽:398
初中數學競賽pdf 瀏覽:568
linux自定義安裝 瀏覽:188
fpic要在每個編譯文件 瀏覽:866
編譯原理廣義推導的定義 瀏覽:911
怎麼在已有的壓縮文件里加密碼 瀏覽:517
安卓手機怎麼設置系統軟體 瀏覽:766
php前端java後端 瀏覽:794
數據框轉換為矩陣python 瀏覽:74
單片機程序反匯編 瀏覽:853
編程和實物不一樣 瀏覽:880
天官賜福小說什麼app可看 瀏覽:208
原車空調改壓縮機 瀏覽:103
python調用其它文件中的函數 瀏覽:484
安卓車載大屏如何下載歌詞 瀏覽:959
刪除這些文件夾 瀏覽:675
新建文件夾怎麼設置快捷搜索 瀏覽:503