1.实现字符串的大小写字母的相互转换。
import java.util.Scanner;
public class Day {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer();
// String s2 = "abcDEF";
Scanner sc = new Scanner(System.in);
String s2 = sc.next();
char c[] = s2.toCharArray();
for (int i = 0; i < s2.length(); i++) {
if (c[i] >= 97) {
s1.append((c[i] + "").toUpperCase());
}
else {
s1.append((c[i] + "").toLowerCase());
}
}
System.out.println(s2);
System.out.println(s1);
}
}
/*
LMwxy
LMwxy
lmWXY
*/
2.分析输入字符串中的单词,并输出其中单词的个数
import java.util.Scanner;
public class Day {
public static void main(String[] args) {
int count = 0;
String sentence = "I am W.X.Y,he is my boyfriend.LM?what's that?";
Scanner s = new Scanner(sentence).useDelimiter(" |,|\\?|\\.");
while (s.hasNext()) {
count++;
System.out.println(s.next());
}
System.out.println("这段短文单词的个数是:" + count);
}
}
/*
I
am
W
X
Y
he
is
my
boyfriend
LM
what's
that
这段短文单词的个数是:12
*/
3.将字符串“.ymene tsrow sih si nam yrevE”反转。
public class Day {
public String convert(String s) {
String ans = "";
if (s == null && s.length() == 0)
return ans;
for (int i = s.length() - 1; i >= 0; i--) {
ans += s.charAt(i);
}
return ans;
}
public static void main(String[] args) {
Day aa = new Day();
String ans = aa.convert(".ymene tsrow sih si nam yrevE");
System.out.println(ans);
}
}
/*
* Every man is his worst enemy.
*/
4.实现输入一个字符串,打印出其中所有的数字
public class Day {
public static void main(String[] args) {
String str = "w11x12ylm22";
str = str.trim();
String str2 = "";
if (str != null && !"".equals(str)) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
str2 += str.charAt(i);
}
}
}
System.out.println(str2);
}
}
/*111222*/
import java.util.Scanner;
public class Day {
public static void main(String args[]) {
System.out.println("输入一个字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuffer str1 = new StringBuffer();
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if (Character.isDigit(a[i])) {// 判断是否为包含数字
str1.append(a[i]);
}
}
System.out.println(str1);
}
}
/*
输入一个字符串:
w1y1xxlm2
112
*/