字符串中各类字符的总和汇总 字符串中子串个数查询实现
2010-10-16 16:23
239 查看
各类字符总和
import java.util.Scanner;
public class StringTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
int big=0,small=0,space=0,other=0;
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if(c>='a' && c<='z')
small++;
if(c>='A' && c<='Z')
big++;
if(c == 32)
space++;
else
other++;
}
System.out.println("小写字母为:" + small + " 大写字母为:"+ big + " 空格为:" + space + " 其他字符为" + other);
}
}
子串个数查询:
使用subString
import java.util.*;
public class StringTest2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
System.out.println("请输入要查询个数的字符串");
String aim= sc.nextLine();
int number = 0;
for(int i = 0;i<=s.length()-aim.length();i++)
{
if(s.substring(i,i+aim.length()).equals(aim))
{
number++;
i = i + aim.length() - 1;
}
}
System.out.println("字符串"+aim+"的个数为"+number+"!!!");
}
}
使用indexOf:
import java.util.*;
public class StringTest3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
System.out.println("请输入要查询个数的字符串");
String aim= sc.nextLine();
int number=0;
int start = 0;
while(s.indexOf(aim,start)>=0 && start < s.length())
{
number++;
start = s.indexOf(aim,start) + aim.length();
}
System.out.println("出现的次数为" + number + "!");
}
}
import java.util.Scanner;
public class StringTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
int big=0,small=0,space=0,other=0;
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if(c>='a' && c<='z')
small++;
if(c>='A' && c<='Z')
big++;
if(c == 32)
space++;
else
other++;
}
System.out.println("小写字母为:" + small + " 大写字母为:"+ big + " 空格为:" + space + " 其他字符为" + other);
}
}
子串个数查询:
使用subString
import java.util.*;
public class StringTest2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
System.out.println("请输入要查询个数的字符串");
String aim= sc.nextLine();
int number = 0;
for(int i = 0;i<=s.length()-aim.length();i++)
{
if(s.substring(i,i+aim.length()).equals(aim))
{
number++;
i = i + aim.length() - 1;
}
}
System.out.println("字符串"+aim+"的个数为"+number+"!!!");
}
}
使用indexOf:
import java.util.*;
public class StringTest3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String s = sc.nextLine();
System.out.println("请输入要查询个数的字符串");
String aim= sc.nextLine();
int number=0;
int start = 0;
while(s.indexOf(aim,start)>=0 && start < s.length())
{
number++;
start = s.indexOf(aim,start) + aim.length();
}
System.out.println("出现的次数为" + number + "!");
}
}
相关文章推荐
- mysql REGEXP 正则的实现两个字符串组的匹配(实现数据库字符串匹配查询)
- 用Hashtable实现字符串中字符的个数的查询
- 2016/1/12 第一题 输出 i 出现次数 第二题 用for循环和if条件句去除字符串中空格 第三题不用endwith 实现尾端字符查询
- 【C语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- [剑指Offer-字符串]请实现一个函数用来找出字符流中第一个只出现一次的字符。
- 回溯字符串【C语言】朴素字符串匹配算法及其实现
- 利用字符出现的次数,编写一个方法,实现基本的字符串压缩功能。
- 查找一个字符串中各个字符出现的次数,用TreeMap实现
- LotusScript查询指定字符在字符串中的位置
- strchr函数的实现,顺序在字符串中寻找所需字符
- Golang编程实现删除字符串中出现次数最少字符的方法
- Js实现找出字符串中出现次数最多的字符
- Oracle实现获得字符串中指定字符个数的方法
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。
- 用js实现读取出字符串中每个字符重复出现的次数?
- C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出【转】
- PHP实现字符串转换成查询语句
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 使用angularjs 去掉字符串最后的几个字符,从而实现trim的操作
- 删除、去掉字符串尾部的0或者某个特定字符(递归实现)