一、将字符串反转
package string;
public class Reverse {
public static void main(String[] args) {
String s1 = "abc defg wf";
//用字符串转字符数组实现反转
String s2 = "";
char[] cs = s1.toCharArray();
for (int i = cs.length - 1; i >= 0; i--) {
s2 = s2 + cs[i];
}
System.out.println("方法一反转操作后为:" + s2);
//利用StringBuffer的reverse()方法实现反转
StringBuffer sb = new StringBuffer(s1);
StringBuffer sb2 = sb.reverse();
System.out.println("方法二反转操作后为:" + sb2);
//利用倒序存储实现反转
String s3=myReverse(s1);
System.out.println("方法三反转操作后为:" + s3);
}
public static String myReverse(String str)
{
char[] arr = new char[str.length()];
int pos = str.length();
for (int x=0; xreturn str;
}
}
输出:
方法一反转操作后为:fw gfed cba
方法二反转操作后为:fw gfed cba
方法三反转操作后为:fw gfed cba
二、实现去字符串两端空格功能
package string;
public class Trime {
public static void main(String[] args) {
String str1 = " dwwg dwg wfg ";
//利用String类自己的方法去除两端空格
String str2 = str1.trim();
System.err.println("方法一"+str2);
//自定义方法去除两端空格
String str3=myTrim(str1);
System.err.println("方法二"+str3);
}
public static String myTrim(String str) {
int start = 0, end = str.length() - 1;
while (start <= end && str.charAt(start) == ' ')
start++;
while (start <= end && str.charAt(end) == ' ')
end--;
return str.substring(start, end + 1);
}
}
输出:
方法一dwwg dwg wfg
方法二dwwg dwg wfg
三、获取一个字符串在另一个字符串中出现的次数,并返回出现起始索引
package string;
public class GetCount {
public static void main(String[] args) {
String str = "sshelloworldhellojavahellojsp";
System.out.println("count=" + getSubCount(str, "hello"));
}
public static int getSubCount(String str, String key) {
int count = 0;
int index = 0;
while ((index = str.indexOf(key, index)) != -1) {
System.out.println("index=" + index);
index = index + key.length();
count++;
}
return count;
}
}
index=2
index=12
index=21
count=3
indexOf返回的是长字符串中所求子字符串起始处索引值,没有则返回-1.
四、将一段英文句子倒序输出
package string;
import java.util.Scanner;
public class Reverse02 {
public static String reverseWords(String sentence) {
StringBuilder sb = new StringBuilder(sentence.length() + 1);
String[] words = sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]).append(' ');
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// 存放输入的字符串
System.out.println("输入测试用例数目n:");
String[] input = new String[in.nextInt()];
in.nextLine();
for (int i = 0; i < input.length; i++) {
input[i] = in.nextLine();
}
// 输出倒序后的英文字符串
System.out.println("输出为:");
for (String s : input) {
System.out.println(reverseWords(s));
}
}
}
输入测试用例数目n:
2
hello world!
i love you?
输出为:
world! hello
you? love i
四、删除一个字符串中出现最多的字符
package string;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DelMost {
public static void main(String[] args) {
String str = "aassdfscfw";
Map map = new HashMap();
char[] box = str.toCharArray();
for (char ch : box) {
Integer i = map.get(ch);
if (i == null) {
map.put(ch, 1);
} else {
map.put(ch, i + 1);
}
} // 创建map键值对
Integer max = 0;
Set set = map.keySet();
for (Character key : set) {
Integer j = map.get(key);
if (max < j) {
max = j;
}
} // 将最大次数传给max
for (Character key : set) {
if (map.get(key) == max) {
str = str.replaceAll(key.toString(), "");
}
}
System.out.println(str);
}
}
aadfcfw
五、输入一行字符分别统计英文字母、数字、空格个数。
package string;
import java.util.Scanner;
public class Tongji {
public static void main(String[] args) {
int eng=0,num=0,kong=0,elsel=0;
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char[] ch=null;
ch=str.toCharArray();
for(char c:ch){
if(c>='0'&&c<='9'){
num++;
}else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
eng++;
}else if(c==' '){
kong++;
}else{
elsel++;
}
}
System.out.println("字母有"+eng);
System.out.println("数字有"+num);
System.out.println("空格有"+kong);
System.out.println("其他"+elsel);
}
}
hello world 378nihao
字母有15
数字有3
空格有2
其他0