String对象小列子

编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数。
编写一个方法,输出在一个字符串中,指定字符串出现的次数。


该列子定义了一个StringIndex 类,实现了两个方法。
import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Administrator
*/
public class StringIndex {
private int i=0;
private int j=0;
private int k=0;

public void getNumber(String s){


// for(int n=0;n<s.length();n++){
// char c=s.charAt(n);
// if(c>='a'&&c<='z'){
// i++;
// }else if(c>='A'&&c<='Z'){
// j++;
// }else{
// k++;
// }
// }
char[] cc=s.toCharArray();
for(int n=0;n<cc.length;n++){
if(cc[n]>='a'&&cc[n]<='z'){
i++;
}else if(cc[n]>='A'&&cc[n]<='Z'){
j++;
}else{
k++;
}
}

System.out.println("该字符串共有小写英文字母数:"+i);
System.out.println("该字符串共有大写英文字母数:"+j);
System.out.println("该字符串共有非英文字母数:"+k);



}

public void getStringNumber(String s,String indexS){


int fromIndex=0;
int n=0;
int k=0;
while(fromIndex<s.length()&& (k =s.indexOf(indexS, fromIndex))!=-1){

fromIndex=k+1;
n++;

}
System.out.println("字符串"+indexS+"在字符串"+s+"中出现了"+n+"次");
}


}


定义测试类:


import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Administrator
*/
public class StringTest {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s=sc.next();
System.out.println(s);
new StringIndex().getNumber(s);
System.out.println("请输入要查询的字符串:");
String indexS=sc.next();
new StringIndex().getStringNumber(s,indexS);


}

}

该列子对中文字符的处理有写小毛病。 :cry:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值