導航:首頁 > 編程語言 > 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字元出現位置相關的資料

熱點內容
反編譯怎麼解決字串長度 瀏覽:279
農行app怎麼凍結賬號 瀏覽:759
mt反編譯的軟體打不開 瀏覽:518
安卓日上免稅APP如何安裝 瀏覽:583
怎麼買星空加密狗 瀏覽:19
北京程序員薪酬 瀏覽:133
華為榮耀暢玩8c升級方舟編譯器 瀏覽:412
手機怎麼把照片填入文件夾 瀏覽:305
php圖片輪換 瀏覽:929
科二學車app軟體哪個好 瀏覽:645
用aj踩東西的解壓視頻 瀏覽:382
將python編譯成手機app 瀏覽:475
程序員社招國企好進么 瀏覽:696
php路徑寫法 瀏覽:432
為什麼有些網頁安卓打不開 瀏覽:220
如何滲透獲取伺服器資料庫 瀏覽:778
ios是怎麼解析app的 瀏覽:256
按揭車怎麼貸款app 瀏覽:477
linux卸載nodejs 瀏覽:128
find方法php 瀏覽:303