我是靠谱客的博主 有魅力乌冬面,这篇文章主要介绍Java中字符串的使用(第二篇),现在分享给大家,希望可以做个参考。

Java中字符串的使用(第二篇)

  • 编写程序,提示用户 输入一个字符串,显示它的长度,第一个字符和最后一个字符
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner; /** * 编写程序,提示用户 输入一个字符串,显示它的长度,第一个字符和最后一个字符 */ public class StringDemo { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("请输入一个字符串:") ; String s = input.nextLine() ; System.out.println("字符串的长度为:" + s.length()) ; System.out.println("字符串的第一个字符为:" + s.charAt(0)) ; System.out.println("字符串的最后一个字符为:" + s.charAt(s.length()-1)) ; } }
  • 提示用户输入两个字符串,判断第二个字符串是否是第一个字符串的子串
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** * 提示用户输入两个字符串,判断第二个字符串是否是第一个字符串的子串 */ import java.util.Scanner; public class SubStringDemo { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("输入第一个字符串:") ; String s1 = input.nextLine() ; System.out.print("输出第二个字符串:") ; String s2 = input.nextLine() ; int result =s1.indexOf(s2) ; if(result >= 0){ System.out.println("s2是s1的子串") ; } else{ System.out.println("s2不是s1的子串") ; } } }
  • 编写方法,统计字符串中包含字母的个数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** * 编写方法,统计字符串中包含字母的个数 */ public class CountLetters { public static int countLetters(String s){ int count = 0 ; for(int i=0; i<s.length(); i++){ if((s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') || (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')){ count ++ ; } } return count ; } public static void main(String[] args){ String s = "this is a 傻子" ; System.out.println(countLetters(s)) ; } }
  • 编写一个方法将十进制数转换成二进制数的字符串
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner; /** * 编写一个方法将十进制数转换成二进制数的字符串 */ public class ToBinary { public static String toBinary(int value){ String s = "" ; String s1 = "" ; while(value != 0){ int r = value % 2 ; s += r ; value = value / 2 ; } for(int i=s.length()-1; i>=0; i--){ s1 += s.charAt(i) ; } return s1 ; } public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("请输入一个10进制的整数:") ; int value = input.nextInt() ; System.out.println(toBinary(value)) ; } }
  • 将字符串转换成字符数组,写一个方法完成字符数组元素的排序输出
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner; /** * 将字符串转换成字符数组,写一个方法完成字符数组元素的排序输出 */ public class Sort { public static String sort(String s){ char [] c = s.toCharArray() ; for(int i=0; i<c.length-1; i++){ for(int j=i+1; j<c.length; j++){ if(c[i] > c[j]){ char temp = c[i] ; c[i] = c[j] ; c[j] = temp ; } } } String str = new String(c) ; return str ; } public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("请输入一个字符串:") ; String s = input.nextLine() ; System.out.println("排序后的字符串:" + sort(s)) ; } }
  • 编写一个程序,要求从键盘输入一个字符串,然后输出加密后的字符串
  • 加密规则:对每个字母转换成下一个字母表示
  • 例如:原来是a,则转换成b,原来是B,则转换成C。
  • 注:小写的z转换成小写的a,大写的A转换成大写的A
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner; /** * 编写一个程序,要求从键盘输入一个字符串,然后输出加密后的字符串 * 加密规则:对每个字母转换成下一个字母表示 * 例如:原来是a,则转换成b,原来是B,则转换成C。 * 注:小写的z转换成小写的a,大写的A转换成大写的A */ public class Encrypt { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("请从输入一个字符串:") ; String s = input.nextLine() ; StringBuilder ss = new StringBuilder(s) ; for(int i=0; i<ss.length(); i++){ char c = ss.charAt(i) ; if(c == 'Z' || c == 'z'){ c = (char)(c - 25) ; } else { c = (char)(c + 1) ; } ss.setCharAt(i,c) ; } System.out.println("加密后的字符串:" + ss) ; } }
  • 将字符串"no pains,no gains."解析成含有4个单词组成的字符串数组
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/** * 将字符串"no pains,no gains."解析成含有4个单词组成的字符串数组 */ public class SplitDemo { public static void main(String[] args){ String s = "no pains, no gains." ; String [] c = new String [4] ; c = s.split("[ ,.]") ; for(String m : c){ System.out.print(m + " ") ; } } }
  • 键盘输入3个城市名,比较城市名字符串的大小,然后按由小到大顺序输出
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner; /** * 键盘输入3个城市名,比较城市名字符串的大小,然后按由小到大顺序输出 */ public class SortDemo { public static void main(String[] args){ Scanner input = new Scanner(System.in) ; System.out.print("请输入三个城市名:") ; String [] cities = new String [3] ; for(int i=0; i<cities.length; i++){ cities[i] = input.next() ; } for(int i=0; i<cities.length-1; i++){ for(int j=i+1; j<cities.length; j++){ if(cities[i].compareTo(cities[j]) > 0){ String temp = cities[i] ; cities[i] = cities[j] ; cities[j] = temp ; } } } for(String s : cities){ System.out.print(s + " ") ; } } }
  • 当前日期的输出
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** * 当前日期的输出 */ import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class LocalDateDemo { public static void main(String[] args){ LocalDate date = LocalDate.now() ; LocalDateTime date_time = LocalDateTime.now(); LocalTime time = LocalTime.now() ; SimpleDateFormat times = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; String time_now = times.format(new java.util.Date()) ; System.out.println("今天的日期是:" + date) ; System.out.println("当前的日期时间是:" + date_time) ; System.out.println("当前的时间是:" + time) ; System.out.println("当前的标准时间是:" + time_now) ; } }

最后

以上就是有魅力乌冬面最近收集整理的关于Java中字符串的使用(第二篇)的全部内容,更多相关Java中字符串内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部