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

熱點內容
安卓怎麼改手機字體樣式 瀏覽:415
蓋世汽車招聘新聞編譯 瀏覽:410
python識別網頁視頻時間 瀏覽:15
如何用dos命令打開文件 瀏覽:208
美國加密貨幣交易委員會 瀏覽:652
變頻壓縮機通用驅動板 瀏覽:621
紅色警戒與命令與征服 瀏覽:782
如何安裝伺服器共享列印機 瀏覽:319
重症監護室解壓 瀏覽:610
python把三列數據拼接起來 瀏覽:504
北京下命令 瀏覽:102
linux啟動postgresql 瀏覽:420
ug編程模板文件夾 瀏覽:68
python怎麼建xml 瀏覽:421
雲伺服器內組網 瀏覽:976
伺服器拔線是什麼意思 瀏覽:50
伺服器管理口帶外管理怎麼弄 瀏覽:212
一天學會php 瀏覽:252
半監督fcm的演算法源碼 瀏覽:129
薄荷健康app運動訓練在哪裡 瀏覽:723