java统计字符串中字母个数
㈠ java判断一个字符串中包含多少个字母
方案一:
Pattern p = Pattern.compile("[a-zA-z]"); if(p.matcher("哈哈哈哈x哈哈").find()) { System.out.println("含有英文字符"); }else{ System.out.println("不含英文字符"); }方案二:
if("哈哈哈哈x哈哈".matches(".*[a-zA-z].*")) { System.out.println("含有英文字符"); }else{ System.out.println("不含英文字符"); }
㈡ java中如何统计某个字母在一个字符串中出现了多少次啊
1、根据length获取字符串长度
Strings="abcdedfae";//定义一个斗友字符串知渗
intlen=s.length();//获取原来的字符串长度
2、通过replaceAll方式,把搭销脊字符串中该字母替换成空
Strings1=s.replaceAll(要统计的字母,"");
3、获取替换后的字符串长度
intlen2=s1.length();
4、原来的字符串长度减去替换后的字符串长度就是该字母出现的次数
intlenTimes=len1-len2;//出现的次数
㈢ 鐢╦ava 缂栧啓涓涓绋嫔簭锛屾帴鍙楃敤鎴疯緭鍏ョ殑涓娈佃嫳鏂囨枃瀛楋纴缁熻″嚭鍏朵腑镄勫瓧绗︿釜鏁般佸崟璇崭釜鏁板拰鍙ュ瓙镄勪釜鏁般
public static void main(String[] args)
{
System.out.println("璇疯緭鍏ヨ嫳璇鐗囨,浠';'缁撴潫:");
Scannerscanner = newScanner(System.in);
String str = "";
int dc = 0;
int zc = 0;
intjc= 0;
while(scanner.hasNext())
{
str = scanner.next();
zc += str.length();
dc++;
System.out.println(str);
if (str.contains("."))
{
jc+= 1;
}
if (str.contains(";"))
{
break;
}
}
// scanner.
System.out.println("鍗曡瘝涓鏁颁负:" + dc);
System.out.println("瀛楁瘝鏁颁负锛" + (zc - 1));
System.out.println("鍙ユ暟涓:" +jc);
}
镓╁𪾢璧勬枡锛
import java.io.*;
public class test
{ public static void main(String[] args) throws IOException
{ System.out.print("璇疯緭鍏ヤ竴涓插瓧姣:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))锛
String str=br.readLine();
int char1=0,letter=0,sentence=0;
for(int i=0;i<str.length();i++)
{
char ch = str.charAt(i);
if(ch!=' ')
{ char1++;
}
if(ch==' '||ch=='/n')
{ //链夌┖镙,锷犱竴鍗曡瘝
if(i!=0&&str.charAt(i-1)!=' ')
letter++;
}
if(ch=='?'||ch=='!'||ch=='.')
{ sentence++;
}
}
System.out.println("瀛楃︽暟锛"+char1);
System.out.println("鍗曡瘝涓鏁:"+letter);
System.out.println("鍙ュ瓙鏁:"+sentence)锛
}
}
import java.io.*;
public class test
{ public static void main(String[] args) throws IOException
{ System.out.print("璇疯緭鍏ヤ竴涓插瓧姣:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int char1=0,letter=0,sentence=0;
for(int i=0;i<str.length();i++)
{
char ch = str.charAt(i);
if(ch!=' ')
{ char1++;
}
if(ch==' '||ch=='/n')
{ //链夌┖镙,锷犱竴鍗曡瘝
if(i!=0&&str.charAt(i-1)!=' ')
letter++;
}
if(ch=='?'||ch=='!'||ch=='.')
{ sentence++;
}
锝
System.out.println("瀛楃︽暟锛"+char1);
System.out.println("鍗曡瘝涓鏁:"+letter);
System.out.println("鍙ュ瓙鏁:"+sentence);
}
}
㈣ 缁熻″瓧绗︽暟缁刟rray涓灏忓啓瀛楁瘝镄勪釜鏁,骞跺皢瀹冧滑杞鍖栨垚澶у啓瀛楁瘝 Java镄勪唬镰佹庝箞鍐椤晩锛
鍙浠ヤ娇鐢ㄤ袱涓鍙橀噺鍒嗗埆璁板綍灏忓啓瀛楁瘝镄勪釜鏁板拰澶у啓瀛楁瘝镄勪釜鏁帮纴铹跺悗阆嶅巻鏁翠釜瀛楃︽暟缁勶纴浣跨敤Java涓镄凛haracter绫荤殑isLowerCase()鍜宼oUpperCase()鏂规硶𨱒ュ垽鏂鍜岃浆鎹㈠皬鍐椤瓧姣嶃傚备笅浠g爜镓绀猴细
char[] array = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int lowerCount = 0;
int upperCount = 0;
for (int i = 0; i < array.length; i++) {
if (Character.isLowerCase(array[i])) {
lowerCount++;
array[i] = Character.toUpperCase(array[i]);
} else {
upperCount++;
}
}
System.out.println("灏忓啓瀛楁瘝涓鏁帮细" + lowerCount);
System.out.println("澶у啓瀛楁瘝涓鏁帮细" + upperCount);
System.out.println("杞鎹㈠悗镄勫瓧绗︽暟缁勶细" + Arrays.toString(array));