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

熱點內容
資料庫查詢系統源碼 瀏覽:611
php5314 瀏覽:350
完美國際安裝到哪個文件夾 瀏覽:663
什麼app可以掃一掃做題 瀏覽:534
程序員編碼論壇 瀏覽:918
淘點是什麼app 瀏覽:654
中國高等植物pdf 瀏覽:448
51單片機時間 瀏覽:176
後台如何獲取伺服器ip 瀏覽:260
單片機流水燈程序c語言 瀏覽:227
程序員第二職業掙錢 瀏覽:234
運行里怎麼輸入伺服器路徑 瀏覽:833
pythonstepwise 瀏覽:502
劉一男詞彙速記指南pdf 瀏覽:56
php認證級別 瀏覽:361
方舟編譯啥時候推送 瀏覽:1003
php手機驗證碼生成 瀏覽:669
哲學思維pdf 瀏覽:9
凌達壓縮機有限公司招聘 瀏覽:527
weblogic命令部署 瀏覽:30