java_第二章 字符串



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
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值