java中 如何统计一段字符串中相同字符的个数

如题所述

第1个回答  2022-12-11

public class CharCount {

public static void main(String[] args){

int count = 0;

String str = "gfjsjgperjtpojewrjopeqjwrio34rengflkajsaljfrajfasnflanfla";

for(int i = 0; i < str.length(); i++){

if(str.charAt(i)=='j'){

count++;

}

}

System.out.println(count);

}

}

扩展资料:

Java常见字符:

int 型

常量:

123,6000(十进制)|| 077(八进制)|| 0x3ABC(十六进制)

声明:

int x =12 ;

占用字节内存:4

取值范围:-2^31--2^31-1

byte 型

常量:

不存在

声明:

byte x = -12 ;

占用字节内存:1

取值范围:-2^7--2^7-1

short 型

常量:

不存在

声明:

short x = 12;

占用字节内存:2

取值范围:-2^15--2^15-1

long 型

常量:

用后缀 L 来表示,如:108L等

声明:

long width = 12L;

占用字节内存:8

取值范围:-2^63--2^63-1

参考资料来源:百度百科--Java (计算机编程语言)

相似回答