String详解(¥20)

字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。

构造方法:

 * public String():空构造
 * public String(byte[] bytes):把字节数组转成字符串
 * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
 * public String(char[] value):把字符数组转成字符串
 * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
 * public String(String original):把字符串常量值转成字符串
 *

 * 字符串的方法:

 * public int length():返回此字符串的长度。


    
    
  1. package String;
  2. /*
  3. * 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
  4. * 通过查看API,我们可以知道
  5. * A:字符串字面值"abc"也可以看成是一个字符串对象。
  6. * B:字符串是常量,一旦被赋值,就不能被改变。
  7. *
  8. * 构造方法:
  9. * public String():空构造
  10. * public String(byte[] bytes):把字节数组转成字符串
  11. * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  12. * public String(char[] value):把字符数组转成字符串
  13. * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  14. * public String(String original):把字符串常量值转成字符串
  15. *
  16. * 字符串的方法:
  17. * public int length():返回此字符串的长度。
  18. */
  19. public class Demo1 {
  20. public static void main(String[] args) {
  21. // public String():空构造
  22. String s1 = new String();
  23. System.out.println( "s1:" + s1);
  24. System.out.println( "s1.length():" + s1.length());
  25. System.out.println( "--------------------------");
  26. // public String(byte[] bytes):把字节数组转成字符串
  27. byte[] bys = { 97, 98, 99, 100, 101 };
  28. String s2 = new String(bys);
  29. System.out.println( "s2:" + s2);
  30. System.out.println( "s2.length():" + s2.length());
  31. System.out.println( "--------------------------");
  32. // public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  33. // 我想得到字符串"bcd"
  34. String s3 = new String(bys, 1, 3);
  35. System.out.println( "s3:" + s3);
  36. System.out.println( "s3.length():" + s3.length());
  37. System.out.println( "--------------------------");
  38. // public String(char[] value):把字符数组转成字符串
  39. char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };
  40. String s4 = new String(chs);
  41. System.out.println( "s4:" + s4);
  42. System.out.println( "s4.length():" + s4.length());
  43. System.out.println( "--------------------------");
  44. // public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  45. String s5 = new String(chs, 2, 4);
  46. System.out.println( "s5:" + s5);
  47. System.out.println( "s5.length():" + s5.length());
  48. System.out.println( "--------------------------");
  49. //public String(String original):把字符串常量值转成字符串
  50. String s6 = new String( "abcde");
  51. System.out.println( "s6:" + s6);
  52. System.out.println( "s6.length():" + s6.length());
  53. System.out.println( "--------------------------");
  54. //字符串字面值"abc"也可以看成是一个字符串对象。
  55. String s7 = "abcde";
  56. System.out.println( "s7:"+s7);
  57. System.out.println( "s7.length():"+s7.length());
  58. }
  59. }

 String s = new String(“hello”)和String s = “hello”;的区别?

有。前者会创建2个对象,后者创建1个对象。
 * 
 * ==:比较引用类型比较的是地址值是否相同

 * equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。


    
    
  1. package String;
  2. /*
  3. * String s = new String(“hello”)和String s = “hello”;的区别?
  4. * 有。前者会创建2个对象,后者创建1个对象。
  5. *
  6. * ==:比较引用类型比较的是地址值是否相同
  7. * equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
  8. */
  9. public class Demo2 {
  10. public static void main(String[] args) {
  11. String s1 = new String( "hello");
  12. String s2 = "hello";
  13. System.out.println(s1 == s2); // false
  14. System.out.println(s1.equals(s2)); // true
  15. }
  16. }

字符串如果是变量相加,先开空间,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建


     
     
  1. package String;
  2. /*
  3. * 看程序写结果
  4. * 字符串如果是变量相加,先开空间,在拼接。
  5. * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
  6. */
  7. public class Demo3 {
  8. public static void main(String[] args) {
  9. String s1 = "hello";
  10. String s2 = "world";
  11. String s3 = "helloworld";
  12. System.out.println(s3 == s1 + s2); // false
  13. System.out.println(s3.equals(s1 + s2)); // true
  14. System.out.println(s3 == "hello" + "world"); // false 这个我们错了,应该是true
  15. System.out.println(s3.equals( "hello" + "world")); // true
  16. // 通过反编译看源码,我们知道这里已经做好了处理。
  17. // System.out.println(s3 == "helloworld");
  18. // System.out.println(s3.equals("helloworld"));
  19. }
  20. }

String类的判断功能:

  * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
 * boolean contains(String str):判断大字符串中是否包含小字符串
 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
 * boolean isEmpty():判断字符串是否为空。

     
     
  1. package String;
  2. /*
  3. * String类的判断功能:
  4. * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  5. * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  6. * boolean contains(String str):判断大字符串中是否包含小字符串
  7. * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  8. * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  9. * boolean isEmpty():判断字符串是否为空。
  10. *
  11. * 注意:
  12. * 字符串内容为空和字符串对象为空。
  13. * String s = "";
  14. * String s = null;
  15. */
  16. public class Demo4 {
  17. public static void main(String[] args) {
  18. // 创建字符串对象
  19. String s1 = "helloworld";
  20. String s2 = "helloworld";
  21. String s3 = "HelloWorld";
  22. // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  23. System.out.println( "equals:" + s1.equals(s2));
  24. System.out.println( "equals:" + s1.equals(s3));
  25. System.out.println( "-----------------------");
  26. // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  27. System.out.println( "equals:" + s1.equalsIgnoreCase(s2));
  28. System.out.println( "equals:" + s1.equalsIgnoreCase(s3));
  29. System.out.println( "-----------------------");
  30. // boolean contains(String str):判断大字符串中是否包含小字符串
  31. System.out.println( "contains:" + s1.contains( "hello"));
  32. System.out.println( "contains:" + s1.contains( "hw"));
  33. System.out.println( "-----------------------");
  34. // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  35. System.out.println( "startsWith:" + s1.startsWith( "h"));
  36. System.out.println( "startsWith:" + s1.startsWith( "hello"));
  37. System.out.println( "startsWith:" + s1.startsWith( "world"));
  38. System.out.println( "-----------------------");
  39. // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
  40. // boolean isEmpty():判断字符串是否为空。
  41. System.out.println( "isEmpty:" + s1.isEmpty());
  42. String s4 = "";
  43. String s5 = ;
  44. System.out.println( "isEmpty:" + s4.isEmpty());
  45. // NullPointerException
  46. // s5对象都不存在,所以不能调用方法,空指针异常
  47. //System.out.println("isEmpty:" + s5.isEmpty());
  48. }
  49. }

String类的获取功能

 * int length():获取字符串的长度。
 * char charAt(int index):获取指定索引位置的字符
 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
 * 为什么这里是int类型,而不是char类型?
 * 原因是:'a'和97其实都可以代表'a'
 * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
 * String substring(int start):从指定位置开始截取字符串,默认到末尾。
 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
 */

     
     
  1. package String;
  2. /*
  3. * String类的获取功能
  4. * int length():获取字符串的长度。
  5. * char charAt(int index):获取指定索引位置的字符
  6. * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  7. * 为什么这里是int类型,而不是char类型?
  8. * 原因是:'a'和97其实都可以代表'a'
  9. * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  10. * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  11. * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  12. * String substring(int start):从指定位置开始截取字符串,默认到末尾。
  13. * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
  14. */
  15. public class Demo5 {
  16. public static void main(String[] args) {
  17. // 定义一个字符串对象
  18. String s = "helloworld";
  19. // int length():获取字符串的长度。
  20. System.out.println( "s.length:" + s.length());
  21. System.out.println( "----------------------");
  22. // char charAt(int index):获取指定索引位置的字符
  23. System.out.println( "charAt:" + s.charAt( 7));
  24. System.out.println( "----------------------");
  25. // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  26. System.out.println( "indexOf:" + s.indexOf( 'l'));
  27. System.out.println( "----------------------");
  28. // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  29. System.out.println( "indexOf:" + s.indexOf( "owo"));
  30. System.out.println( "----------------------");
  31. // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  32. System.out.println( "indexOf:" + s.indexOf( 'l', 4));
  33. System.out.println( "indexOf:" + s.indexOf( 'k', 4)); // -1
  34. System.out.println( "indexOf:" + s.indexOf( 'l', 40)); // -1
  35. System.out.println( "----------------------");
  36. // 自己练习:int indexOf(String str,int
  37. // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  38. // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
  39. System.out.println( "substring:" + s.substring( 5));
  40. System.out.println( "substring:" + s.substring( 0));
  41. System.out.println( "----------------------");
  42. // String substring(int start,int
  43. // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
  44. System.out.println( "substring:" + s.substring( 3, 8));
  45. System.out.println( "substring:" + s.substring( 0, s.length()));
  46. }
  47. }

需求:遍历获取字符串中的每一个字符

 * 
 * 分析:
 * A:如何能够拿到每一个字符呢?
 * char charAt(int index)
 * B:我怎么知道字符到底有多少个呢?
 * int length()

     
     
  1. package String;
  2. /*
  3. * 需求:遍历获取字符串中的每一个字符
  4. *
  5. * 分析:
  6. * A:如何能够拿到每一个字符呢?
  7. * char charAt(int index)
  8. * B:我怎么知道字符到底有多少个呢?
  9. * int length()
  10. */
  11. public class Demo6 {
  12. public static void main(String[] args) {
  13. // 定义字符串
  14. String s = "helloworld";
  15. // 原始版本
  16. // System.out.println(s.charAt(0));
  17. // System.out.println(s.charAt(1));
  18. // System.out.println(s.charAt(2));
  19. // System.out.println(s.charAt(3));
  20. // System.out.println(s.charAt(4));
  21. // System.out.println(s.charAt(5));
  22. // System.out.println(s.charAt(6));
  23. // System.out.println(s.charAt(7));
  24. // System.out.println(s.charAt(8));
  25. // System.out.println(s.charAt(9));
  26. // 只需要我们从0取到9
  27. // for (int x = 0; x < 10; x++) {
  28. // System.out.println(s.charAt(x));
  29. // }
  30. // 如果长度特别长,我不可能去数,所以我们要用长度功能
  31. for ( int x = 0; x < s.length(); x++) {
  32. // char ch = s.charAt(x);
  33. // System.out.println(ch);
  34. // 仅仅是输出,我就直接输出了
  35. System.out.println(s.charAt(x));
  36. }
  37. }
  38. }

需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

 * 举例:
 * "Hello123World"
 * 结果:
 * 大写字符:2个
 * 小写字符:8个
 * 数字字符:3个
 * 
 * 分析:
 * 前提:字符串要存在
 * A:定义三个统计变量
 * bigCount=0
 * smallCount=0
 * numberCount=0
 * B:遍历字符串,得到每一个字符。
 * length()和charAt()结合
 * C:判断该字符到底是属于那种类型的
 * 大:bigCount++
 * 小:smallCount++
 * 数字:numberCount++
 * 
 * 这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
 * ASCII码表:
 * 0 48
 * A 65
 * a 97
 * 虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
 * char ch = s.charAt(x);
 * 
 * if(ch>='0' && ch<='9') numberCount++
 * if(ch>='a' && ch<='z') smallCount++
 * if(ch>='A' && ch<='Z') bigCount++
 * D:输出结果。

     
     
  1. package String;
  2. /*
  3. * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
  4. * 举例:
  5. * "Hello123World"
  6. * 结果:
  7. * 大写字符:2个
  8. * 小写字符:8个
  9. * 数字字符:3个
  10. *
  11. * 分析:
  12. * 前提:字符串要存在
  13. * A:定义三个统计变量
  14. * bigCount=0
  15. * smallCount=0
  16. * numberCount=0
  17. * B:遍历字符串,得到每一个字符。
  18. * length()和charAt()结合
  19. * C:判断该字符到底是属于那种类型的
  20. * 大:bigCount++
  21. * 小:smallCount++
  22. * 数字:numberCount++
  23. *
  24. * 这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
  25. * ASCII码表:
  26. * 0 48
  27. * A 65
  28. * a 97
  29. * 虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
  30. * char ch = s.charAt(x);
  31. *
  32. * if(ch>='0' && ch<='9') numberCount++
  33. * if(ch>='a' && ch<='z') smallCount++
  34. * if(ch>='A' && ch<='Z') bigCount++
  35. * D:输出结果。
  36. *
  37. * 练习:把给定字符串的方式,改进为键盘录入字符串的方式。
  38. */
  39. public class Demo7 {
  40. public static void main(String[] args) {
  41. //定义一个字符串
  42. String s = "Hello123World";
  43. //定义三个统计变量
  44. int bigCount = 0;
  45. int smallCount = 0;
  46. int numberCount = 0;
  47. //遍历字符串,得到每一个字符。
  48. for( int x= 0; x<s.length(); x++){
  49. char ch = s.charAt(x);
  50. //判断该字符到底是属于那种类型的
  51. if(ch>= 'a' && ch<= 'z'){
  52. smallCount++;
  53. } else if(ch>= 'A' && ch<= 'Z'){
  54. bigCount++;
  55. } else if(ch>= '0' && ch<= '9'){
  56. numberCount++;
  57. }
  58. }
  59. //输出结果。
  60. System.out.println( "大写字母"+bigCount+ "个");
  61. System.out.println( "小写字母"+smallCount+ "个");
  62. System.out.println( "数字"+numberCount+ "个");
  63. }
  64. }

String的转换功能:

 * byte[] getBytes():把字符串转换为字节数组。
 * char[] toCharArray():把字符串转换为字符数组。
 * static String valueOf(char[] chs):把字符数组转成字符串。
 * static String valueOf(int i):把int类型的数据转成字符串。
 * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
 * String toLowerCase():把字符串转成小写。
 * String toUpperCase():把字符串转成大写。
 * String concat(String str):把字符串拼接。

     
     
  1. package String;
  2. /*
  3. * String的转换功能:
  4. * byte[] getBytes():把字符串转换为字节数组。
  5. * char[] toCharArray():把字符串转换为字符数组。
  6. * static String valueOf(char[] chs):把字符数组转成字符串。
  7. * static String valueOf(int i):把int类型的数据转成字符串。
  8. * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
  9. * String toLowerCase():把字符串转成小写。
  10. * String toUpperCase():把字符串转成大写。
  11. * String concat(String str):把字符串拼接。
  12. */
  13. public class Demo8 {
  14. public static void main(String[] args) {
  15. // 定义一个字符串对象
  16. String s = "JavaSE";
  17. // byte[] getBytes():把字符串转换为字节数组。
  18. byte[] bys = s.getBytes();
  19. for ( int x = 0; x < bys.length; x++) {
  20. System.out.println(bys[x]);
  21. }
  22. System.out.println( "----------------");
  23. // char[] toCharArray():把字符串转换为字符数组。
  24. char[] chs = s.toCharArray();
  25. for ( int x = 0; x < chs.length; x++) {
  26. System.out.println(chs[x]);
  27. }
  28. System.out.println( "----------------");
  29. // static String valueOf(char[] chs):把字符数组转成字符串。
  30. String ss = String.valueOf(chs);
  31. System.out.println(ss);
  32. System.out.println( "----------------");
  33. // static String valueOf(int i):把int类型的数据转成字符串。
  34. int i = 100;
  35. String sss = String.valueOf(i);
  36. System.out.println(sss);
  37. System.out.println( "----------------");
  38. // String toLowerCase():把字符串转成小写。
  39. System.out.println( "toLowerCase:" + s.toLowerCase());
  40. System.out.println( "s:" + s);
  41. // System.out.println("----------------");
  42. // String toUpperCase():把字符串转成大写。
  43. System.out.println( "toUpperCase:" + s.toUpperCase());
  44. System.out.println( "----------------");
  45. // String concat(String str):把字符串拼接。
  46. String s1 = "hello";
  47. String s2 = "world";
  48. String s3 = s1 + s2;
  49. String s4 = s1.concat(s2);
  50. System.out.println( "s3:"+s3);
  51. System.out.println( "s4:"+s4);
  52. }
  53. }

需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

* 举例:
 * helloWORLD
 * 结果:
 * Helloworld
 * 
 * 分析:
 * A:先获取第一个字符
 * B:获取除了第一个字符以外的字符
 * C:把A转成大写
 * D:把B转成小写
 * E:C拼接D

     
     
  1. package String;
  2. /*
  3. * 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
  4. * 举例:
  5. * helloWORLD
  6. * 结果:
  7. * Helloworld
  8. *
  9. * 分析:
  10. * A:先获取第一个字符
  11. * B:获取除了第一个字符以外的字符
  12. * C:把A转成大写
  13. * D:把B转成小写
  14. * E:C拼接D
  15. */
  16. public class Demo9 {
  17. public static void main(String[] args) {
  18. // 定义一个字符串
  19. String s = "helloWORLD";
  20. // 先获取第一个字符
  21. String s1 = s.substring( 0, 1);
  22. // 获取除了第一个字符以外的字符
  23. String s2 = s.substring( 1);
  24. // 把A转成大写
  25. String s3 = s1.toUpperCase();
  26. // 把B转成小写
  27. String s4 = s2.toLowerCase();
  28. // C拼接D
  29. String s5 = s3.concat(s4);
  30. System.out.println(s5);
  31. // 优化后的代码
  32. // 链式编程
  33. String result = s.substring( 0, 1).toUpperCase()
  34. .concat(s.substring( 1).toLowerCase());
  35. System.out.println(result);
  36. }
  37. }

String类的其他功能:

 * 
 * 替换功能:
 * String replace(char old,char new)
 * String replace(String old,String new)
 *
 * 去除字符串两空格
 * String trim()
 * 
 * 按字典顺序比较两个字符串  
 * int compareTo(String str)
 * int compareToIgnoreCase(String str)

     
     
  1. package String;
  2. /*
  3. * String类的其他功能:
  4. *
  5. * 替换功能:
  6. * String replace(char old,char new)
  7. * String replace(String old,String new)
  8. *
  9. * 去除字符串两空格
  10. * String trim()
  11. *
  12. * 按字典顺序比较两个字符串
  13. * int compareTo(String str)
  14. * int compareToIgnoreCase(String str)
  15. */
  16. public class Demo10 {
  17. public static void main(String[] args) {
  18. // 替换功能
  19. String s1 = "helloworld";
  20. String s2 = s1.replace( 'l', 'k');
  21. String s3 = s1.replace( "owo", "ak47");
  22. System.out.println( "s1:" + s1);
  23. System.out.println( "s2:" + s2);
  24. System.out.println( "s3:" + s3);
  25. System.out.println( "---------------");
  26. // 去除字符串两空格
  27. String s4 = " hello world ";
  28. String s5 = s4.trim();
  29. System.out.println( "s4:" + s4 + "---");
  30. System.out.println( "s5:" + s5 + "---");
  31. // 按字典顺序比较两个字符串
  32. String s6 = "hello";
  33. String s7 = "hello";
  34. String s8 = "abc";
  35. String s9 = "xyz";
  36. System.out.println(s6.compareTo(s7)); // 0
  37. System.out.println(s6.compareTo(s8)); // 7
  38. System.out.println(s6.compareTo(s9)); // -16
  39. }
  40. }

需求:把数组中的数据按照指定个格式拼接成一个字符串

 * 举例:
 * int[] arr = {1,2,3};
 * 输出结果:
 * "[1, 2, 3]"
 * 分析:
 * A:定义一个字符串对象,只不过内容为空
 * B:先把字符串拼接一个"["
 * C:遍历int数组,得到每一个元素
 * D:先判断该元素是否为最后一个
 * 是:就直接拼接元素和"]"
 * 不是:就拼接元素和逗号以及空格
 * E:输出拼接后的字符串

     
     
  1. package String;
  2. /*
  3. * 需求:把数组中的数据按照指定个格式拼接成一个字符串
  4. * 举例:
  5. * int[] arr = {1,2,3};
  6. * 输出结果:
  7. * "[1, 2, 3]"
  8. * 分析:
  9. * A:定义一个字符串对象,只不过内容为空
  10. * B:先把字符串拼接一个"["
  11. * C:遍历int数组,得到每一个元素
  12. * D:先判断该元素是否为最后一个
  13. * 是:就直接拼接元素和"]"
  14. * 不是:就拼接元素和逗号以及空格
  15. * E:输出拼接后的字符串
  16. */
  17. public class demo11 {
  18. public static void main(String[] args) {
  19. // 前提是数组已经存在
  20. int[] arr = { 1, 2, 3 };
  21. // 定义一个字符串对象,只不过内容为空
  22. String s = "";
  23. // 先把字符串拼接一个"["
  24. s += "[";
  25. // 遍历int数组,得到每一个元素
  26. for ( int x = 0; x < arr.length; x++) {
  27. // 先判断该元素是否为最后一个
  28. if (x == arr.length - 1) {
  29. // 就直接拼接元素和"]"
  30. s += arr[x];
  31. s += "]";
  32. } else {
  33. // 就拼接元素和逗号以及空格
  34. s += arr[x];
  35. s += ", ";
  36. }
  37. }
  38. // 输出拼接后的字符串
  39. System.out.println( "最终的字符串是:" + s);
  40. }
  41. }

字符串反转

 * 举例:键盘录入”abc”
 * 输出结果:” cba
 * 
 * 分析:
 * A:键盘录入一个字符串
 * B:定义一个新字符串
 * C:倒着遍历字符串,得到每一个字符
 * a:length()和charAt()结合
 * b:把字符串转成字符数组
 * D:用新字符串把每一个字符拼接起来
 * E:输出新串

     
     
  1. package String;
  2. /*
  3. * 字符串反转
  4. * 举例:键盘录入”abc”
  5. * 输出结果:”cba”
  6. *
  7. * 分析:
  8. * A:键盘录入一个字符串
  9. * B:定义一个新字符串
  10. * C:倒着遍历字符串,得到每一个字符
  11. * a:length()和charAt()结合
  12. * b:把字符串转成字符数组
  13. * D:用新字符串把每一个字符拼接起来
  14. * E:输出新串
  15. */
  16. public class Demo12 {
  17. public static void main(String[] args) {
  18. // 键盘录入一个字符串
  19. Scanner sc = new Scanner(System.in);
  20. System.out.println( "请输入一个字符串:");
  21. String line = sc.nextLine();
  22. /*
  23. // 定义一个新字符串
  24. String result = "";
  25. // 把字符串转成字符数组
  26. char[] chs = line.toCharArray();
  27. // 倒着遍历字符串,得到每一个字符
  28. for (int x = chs.length - 1; x >= 0; x--) {
  29. // 用新字符串把每一个字符拼接起来
  30. result += chs[x];
  31. }
  32. // 输出新串
  33. System.out.println("反转后的结果是:" + result);
  34. */
  35. // 改进为功能实现
  36. String s = myReverse(line);
  37. System.out.println( "实现功能后的结果是:" + s);
  38. }
  39. /*
  40. * 两个明确: 返回值类型:String 参数列表:String
  41. */
  42. public static String myReverse(String s) {
  43. // 定义一个新字符串
  44. String result = "";
  45. // 把字符串转成字符数组
  46. char[] chs = s.toCharArray();
  47. // 倒着遍历字符串,得到每一个字符
  48. for ( int x = chs.length - 1; x >= 0; x--) {
  49. // 用新字符串把每一个字符拼接起来
  50. result += chs[x];
  51. }
  52. return result;
  53. }
  54. }

统计大串中小串出现的次数


    
    
  1. package String;
  2. /*
  3. * 统计大串中小串出现的次数
  4. * 举例:
  5. * 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
  6. * 结果:
  7. * java出现了5次
  8. *
  9. * 分析:
  10. * 前提:是已经知道了大串和小串。
  11. *
  12. * A:定义一个统计变量,初始化值是0
  13. * B:先在大串中查找一次小串第一次出现的位置
  14. * a:索引是-1,说明不存在了,就返回统计变量
  15. * b:索引不是-1,说明存在,统计变量++
  16. * C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
  17. * D:回到B
  18. */
  19. public class Demo13 {
  20. public static void main(String[] args) {
  21. // 定义大串
  22. String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
  23. // 定义小串
  24. String minString = "java";
  25. // 写功能实现
  26. int count = getCount(maxString, minString);
  27. System.out.println( "Java在大串中出现了:" + count + "次");
  28. }
  29. /*
  30. * 两个明确: 返回值类型:int 参数列表:两个字符串
  31. */
  32. public static int getCount(String maxString, String minString) {
  33. // 定义一个统计变量,初始化值是0
  34. int count = 0;
  35. /*
  36. // 先在大串中查找一次小串第一次出现的位置
  37. int index = maxString.indexOf(minString);
  38. // 索引不是-1,说明存在,统计变量++
  39. while (index != -1) {
  40. count++;
  41. // 把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
  42. // int startIndex = index + minString.length();
  43. // maxString = maxString.substring(startIndex);
  44. maxString = maxString.substring(index + minString.length());
  45. // 继续查
  46. index = maxString.indexOf(minString);
  47. }
  48. */
  49. int index;
  50. //先查,赋值,判断
  51. while((index=maxString.indexOf(minString))!=- 1){
  52. count++;
  53. maxString = maxString.substring(index + minString.length());
  54. }
  55. return count;
  56. }
  57. }

字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。

构造方法:

 * public String():空构造
 * public String(byte[] bytes):把字节数组转成字符串
 * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
 * public String(char[] value):把字符数组转成字符串
 * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
 * public String(String original):把字符串常量值转成字符串
 *

 * 字符串的方法:

 * public int length():返回此字符串的长度。


  
  
  1. package String;
  2. /*
  3. * 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
  4. * 通过查看API,我们可以知道
  5. * A:字符串字面值"abc"也可以看成是一个字符串对象。
  6. * B:字符串是常量,一旦被赋值,就不能被改变。
  7. *
  8. * 构造方法:
  9. * public String():空构造
  10. * public String(byte[] bytes):把字节数组转成字符串
  11. * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  12. * public String(char[] value):把字符数组转成字符串
  13. * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  14. * public String(String original):把字符串常量值转成字符串
  15. *
  16. * 字符串的方法:
  17. * public int length():返回此字符串的长度。
  18. */
  19. public class Demo1 {
  20. public static void main(String[] args) {
  21. // public String():空构造
  22. String s1 = new String();
  23. System.out.println( "s1:" + s1);
  24. System.out.println( "s1.length():" + s1.length());
  25. System.out.println( "--------------------------");
  26. // public String(byte[] bytes):把字节数组转成字符串
  27. byte[] bys = { 97, 98, 99, 100, 101 };
  28. String s2 = new String(bys);
  29. System.out.println( "s2:" + s2);
  30. System.out.println( "s2.length():" + s2.length());
  31. System.out.println( "--------------------------");
  32. // public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  33. // 我想得到字符串"bcd"
  34. String s3 = new String(bys, 1, 3);
  35. System.out.println( "s3:" + s3);
  36. System.out.println( "s3.length():" + s3.length());
  37. System.out.println( "--------------------------");
  38. // public String(char[] value):把字符数组转成字符串
  39. char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };
  40. String s4 = new String(chs);
  41. System.out.println( "s4:" + s4);
  42. System.out.println( "s4.length():" + s4.length());
  43. System.out.println( "--------------------------");
  44. // public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  45. String s5 = new String(chs, 2, 4);
  46. System.out.println( "s5:" + s5);
  47. System.out.println( "s5.length():" + s5.length());
  48. System.out.println( "--------------------------");
  49. //public String(String original):把字符串常量值转成字符串
  50. String s6 = new String( "abcde");
  51. System.out.println( "s6:" + s6);
  52. System.out.println( "s6.length():" + s6.length());
  53. System.out.println( "--------------------------");
  54. //字符串字面值"abc"也可以看成是一个字符串对象。
  55. String s7 = "abcde";
  56. System.out.println( "s7:"+s7);
  57. System.out.println( "s7.length():"+s7.length());
  58. }
  59. }

 String s = new String(“hello”)和String s = “hello”;的区别?

有。前者会创建2个对象,后者创建1个对象。
 * 
 * ==:比较引用类型比较的是地址值是否相同

 * equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。


  
  
  1. package String;
  2. /*
  3. * String s = new String(“hello”)和String s = “hello”;的区别?
  4. * 有。前者会创建2个对象,后者创建1个对象。
  5. *
  6. * ==:比较引用类型比较的是地址值是否相同
  7. * equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
  8. */
  9. public class Demo2 {
  10. public static void main(String[] args) {
  11. String s1 = new String( "hello");
  12. String s2 = "hello";
  13. System.out.println(s1 == s2); // false
  14. System.out.println(s1.equals(s2)); // true
  15. }
  16. }

字符串如果是变量相加,先开空间,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建


   
   
  1. package String;
  2. /*
  3. * 看程序写结果
  4. * 字符串如果是变量相加,先开空间,在拼接。
  5. * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
  6. */
  7. public class Demo3 {
  8. public static void main(String[] args) {
  9. String s1 = "hello";
  10. String s2 = "world";
  11. String s3 = "helloworld";
  12. System.out.println(s3 == s1 + s2); // false
  13. System.out.println(s3.equals(s1 + s2)); // true
  14. System.out.println(s3 == "hello" + "world"); // false 这个我们错了,应该是true
  15. System.out.println(s3.equals( "hello" + "world")); // true
  16. // 通过反编译看源码,我们知道这里已经做好了处理。
  17. // System.out.println(s3 == "helloworld");
  18. // System.out.println(s3.equals("helloworld"));
  19. }
  20. }

String类的判断功能:

  * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
 * boolean contains(String str):判断大字符串中是否包含小字符串
 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
 * boolean isEmpty():判断字符串是否为空。

   
   
  1. package String;
  2. /*
  3. * String类的判断功能:
  4. * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  5. * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  6. * boolean contains(String str):判断大字符串中是否包含小字符串
  7. * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  8. * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  9. * boolean isEmpty():判断字符串是否为空。
  10. *
  11. * 注意:
  12. * 字符串内容为空和字符串对象为空。
  13. * String s = "";
  14. * String s = null;
  15. */
  16. public class Demo4 {
  17. public static void main(String[] args) {
  18. // 创建字符串对象
  19. String s1 = "helloworld";
  20. String s2 = "helloworld";
  21. String s3 = "HelloWorld";
  22. // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  23. System.out.println( "equals:" + s1.equals(s2));
  24. System.out.println( "equals:" + s1.equals(s3));
  25. System.out.println( "-----------------------");
  26. // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  27. System.out.println( "equals:" + s1.equalsIgnoreCase(s2));
  28. System.out.println( "equals:" + s1.equalsIgnoreCase(s3));
  29. System.out.println( "-----------------------");
  30. // boolean contains(String str):判断大字符串中是否包含小字符串
  31. System.out.println( "contains:" + s1.contains( "hello"));
  32. System.out.println( "contains:" + s1.contains( "hw"));
  33. System.out.println( "-----------------------");
  34. // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  35. System.out.println( "startsWith:" + s1.startsWith( "h"));
  36. System.out.println( "startsWith:" + s1.startsWith( "hello"));
  37. System.out.println( "startsWith:" + s1.startsWith( "world"));
  38. System.out.println( "-----------------------");
  39. // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
  40. // boolean isEmpty():判断字符串是否为空。
  41. System.out.println( "isEmpty:" + s1.isEmpty());
  42. String s4 = "";
  43. String s5 = ;
  44. System.out.println( "isEmpty:" + s4.isEmpty());
  45. // NullPointerException
  46. // s5对象都不存在,所以不能调用方法,空指针异常
  47. //System.out.println("isEmpty:" + s5.isEmpty());
  48. }
  49. }

String类的获取功能

 * int length():获取字符串的长度。
 * char charAt(int index):获取指定索引位置的字符
 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
 * 为什么这里是int类型,而不是char类型?
 * 原因是:'a'和97其实都可以代表'a'
 * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
 * String substring(int start):从指定位置开始截取字符串,默认到末尾。
 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
 */

   
   
  1. package String;
  2. /*
  3. * String类的获取功能
  4. * int length():获取字符串的长度。
  5. * char charAt(int index):获取指定索引位置的字符
  6. * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  7. * 为什么这里是int类型,而不是char类型?
  8. * 原因是:'a'和97其实都可以代表'a'
  9. * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  10. * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  11. * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  12. * String substring(int start):从指定位置开始截取字符串,默认到末尾。
  13. * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
  14. */
  15. public class Demo5 {
  16. public static void main(String[] args) {
  17. // 定义一个字符串对象
  18. String s = "helloworld";
  19. // int length():获取字符串的长度。
  20. System.out.println( "s.length:" + s.length());
  21. System.out.println( "----------------------");
  22. // char charAt(int index):获取指定索引位置的字符
  23. System.out.println( "charAt:" + s.charAt( 7));
  24. System.out.println( "----------------------");
  25. // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  26. System.out.println( "indexOf:" + s.indexOf( 'l'));
  27. System.out.println( "----------------------");
  28. // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  29. System.out.println( "indexOf:" + s.indexOf( "owo"));
  30. System.out.println( "----------------------");
  31. // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  32. System.out.println( "indexOf:" + s.indexOf( 'l', 4));
  33. System.out.println( "indexOf:" + s.indexOf( 'k', 4)); // -1
  34. System.out.println( "indexOf:" + s.indexOf( 'l', 40)); // -1
  35. System.out.println( "----------------------");
  36. // 自己练习:int indexOf(String str,int
  37. // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  38. // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
  39. System.out.println( "substring:" + s.substring( 5));
  40. System.out.println( "substring:" + s.substring( 0));
  41. System.out.println( "----------------------");
  42. // String substring(int start,int
  43. // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
  44. System.out.println( "substring:" + s.substring( 3, 8));
  45. System.out.println( "substring:" + s.substring( 0, s.length()));
  46. }
  47. }

需求:遍历获取字符串中的每一个字符

 * 
 * 分析:
 * A:如何能够拿到每一个字符呢?
 * char charAt(int index)
 * B:我怎么知道字符到底有多少个呢?
 * int length()

   
   
  1. package String;
  2. /*
  3. * 需求:遍历获取字符串中的每一个字符
  4. *
  5. * 分析:
  6. * A:如何能够拿到每一个字符呢?
  7. * char charAt(int index)
  8. * B:我怎么知道字符到底有多少个呢?
  9. * int length()
  10. */
  11. public class Demo6 {
  12. public static void main(String[] args) {
  13. // 定义字符串
  14. String s = "helloworld";
  15. // 原始版本
  16. // System.out.println(s.charAt(0));
  17. // System.out.println(s.charAt(1));
  18. // System.out.println(s.charAt(2));
  19. // System.out.println(s.charAt(3));
  20. // System.out.println(s.charAt(4));
  21. // System.out.println(s.charAt(5));
  22. // System.out.println(s.charAt(6));
  23. // System.out.println(s.charAt(7));
  24. // System.out.println(s.charAt(8));
  25. // System.out.println(s.charAt(9));
  26. // 只需要我们从0取到9
  27. // for (int x = 0; x < 10; x++) {
  28. // System.out.println(s.charAt(x));
  29. // }
  30. // 如果长度特别长,我不可能去数,所以我们要用长度功能
  31. for ( int x = 0; x < s.length(); x++) {
  32. // char ch = s.charAt(x);
  33. // System.out.println(ch);
  34. // 仅仅是输出,我就直接输出了
  35. System.out.println(s.charAt(x));
  36. }
  37. }
  38. }

需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

 * 举例:
 * "Hello123World"
 * 结果:
 * 大写字符:2个
 * 小写字符:8个
 * 数字字符:3个
 * 
 * 分析:
 * 前提:字符串要存在
 * A:定义三个统计变量
 * bigCount=0
 * smallCount=0
 * numberCount=0
 * B:遍历字符串,得到每一个字符。
 * length()和charAt()结合
 * C:判断该字符到底是属于那种类型的
 * 大:bigCount++
 * 小:smallCount++
 * 数字:numberCount++
 * 
 * 这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
 * ASCII码表:
 * 0 48
 * A 65
 * a 97
 * 虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
 * char ch = s.charAt(x);
 * 
 * if(ch>='0' && ch<='9') numberCount++
 * if(ch>='a' && ch<='z') smallCount++
 * if(ch>='A' && ch<='Z') bigCount++
 * D:输出结果。

   
   
  1. package String;
  2. /*
  3. * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
  4. * 举例:
  5. * "Hello123World"
  6. * 结果:
  7. * 大写字符:2个
  8. * 小写字符:8个
  9. * 数字字符:3个
  10. *
  11. * 分析:
  12. * 前提:字符串要存在
  13. * A:定义三个统计变量
  14. * bigCount=0
  15. * smallCount=0
  16. * numberCount=0
  17. * B:遍历字符串,得到每一个字符。
  18. * length()和charAt()结合
  19. * C:判断该字符到底是属于那种类型的
  20. * 大:bigCount++
  21. * 小:smallCount++
  22. * 数字:numberCount++
  23. *
  24. * 这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。
  25. * ASCII码表:
  26. * 0 48
  27. * A 65
  28. * a 97
  29. * 虽然,我们按照数字的这种比较是可以的,但是想多了,有比这还简单的
  30. * char ch = s.charAt(x);
  31. *
  32. * if(ch>='0' && ch<='9') numberCount++
  33. * if(ch>='a' && ch<='z') smallCount++
  34. * if(ch>='A' && ch<='Z') bigCount++
  35. * D:输出结果。
  36. *
  37. * 练习:把给定字符串的方式,改进为键盘录入字符串的方式。
  38. */
  39. public class Demo7 {
  40. public static void main(String[] args) {
  41. //定义一个字符串
  42. String s = "Hello123World";
  43. //定义三个统计变量
  44. int bigCount = 0;
  45. int smallCount = 0;
  46. int numberCount = 0;
  47. //遍历字符串,得到每一个字符。
  48. for( int x= 0; x<s.length(); x++){
  49. char ch = s.charAt(x);
  50. //判断该字符到底是属于那种类型的
  51. if(ch>= 'a' && ch<= 'z'){
  52. smallCount++;
  53. } else if(ch>= 'A' && ch<= 'Z'){
  54. bigCount++;
  55. } else if(ch>= '0' && ch<= '9'){
  56. numberCount++;
  57. }
  58. }
  59. //输出结果。
  60. System.out.println( "大写字母"+bigCount+ "个");
  61. System.out.println( "小写字母"+smallCount+ "个");
  62. System.out.println( "数字"+numberCount+ "个");
  63. }
  64. }

String的转换功能:

 * byte[] getBytes():把字符串转换为字节数组。
 * char[] toCharArray():把字符串转换为字符数组。
 * static String valueOf(char[] chs):把字符数组转成字符串。
 * static String valueOf(int i):把int类型的数据转成字符串。
 * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
 * String toLowerCase():把字符串转成小写。
 * String toUpperCase():把字符串转成大写。
 * String concat(String str):把字符串拼接。

   
   
  1. package String;
  2. /*
  3. * String的转换功能:
  4. * byte[] getBytes():把字符串转换为字节数组。
  5. * char[] toCharArray():把字符串转换为字符数组。
  6. * static String valueOf(char[] chs):把字符数组转成字符串。
  7. * static String valueOf(int i):把int类型的数据转成字符串。
  8. * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
  9. * String toLowerCase():把字符串转成小写。
  10. * String toUpperCase():把字符串转成大写。
  11. * String concat(String str):把字符串拼接。
  12. */
  13. public class Demo8 {
  14. public static void main(String[] args) {
  15. // 定义一个字符串对象
  16. String s = "JavaSE";
  17. // byte[] getBytes():把字符串转换为字节数组。
  18. byte[] bys = s.getBytes();
  19. for ( int x = 0; x < bys.length; x++) {
  20. System.out.println(bys[x]);
  21. }
  22. System.out.println( "----------------");
  23. // char[] toCharArray():把字符串转换为字符数组。
  24. char[] chs = s.toCharArray();
  25. for ( int x = 0; x < chs.length; x++) {
  26. System.out.println(chs[x]);
  27. }
  28. System.out.println( "----------------");
  29. // static String valueOf(char[] chs):把字符数组转成字符串。
  30. String ss = String.valueOf(chs);
  31. System.out.println(ss);
  32. System.out.println( "----------------");
  33. // static String valueOf(int i):把int类型的数据转成字符串。
  34. int i = 100;
  35. String sss = String.valueOf(i);
  36. System.out.println(sss);
  37. System.out.println( "----------------");
  38. // String toLowerCase():把字符串转成小写。
  39. System.out.println( "toLowerCase:" + s.toLowerCase());
  40. System.out.println( "s:" + s);
  41. // System.out.println("----------------");
  42. // String toUpperCase():把字符串转成大写。
  43. System.out.println( "toUpperCase:" + s.toUpperCase());
  44. System.out.println( "----------------");
  45. // String concat(String str):把字符串拼接。
  46. String s1 = "hello";
  47. String s2 = "world";
  48. String s3 = s1 + s2;
  49. String s4 = s1.concat(s2);
  50. System.out.println( "s3:"+s3);
  51. System.out.println( "s4:"+s4);
  52. }
  53. }

需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

* 举例:
 * helloWORLD
 * 结果:
 * Helloworld
 * 
 * 分析:
 * A:先获取第一个字符
 * B:获取除了第一个字符以外的字符
 * C:把A转成大写
 * D:把B转成小写
 * E:C拼接D

   
   
  1. package String;
  2. /*
  3. * 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
  4. * 举例:
  5. * helloWORLD
  6. * 结果:
  7. * Helloworld
  8. *
  9. * 分析:
  10. * A:先获取第一个字符
  11. * B:获取除了第一个字符以外的字符
  12. * C:把A转成大写
  13. * D:把B转成小写
  14. * E:C拼接D
  15. */
  16. public class Demo9 {
  17. public static void main(String[] args) {
  18. // 定义一个字符串
  19. String s = "helloWORLD";
  20. // 先获取第一个字符
  21. String s1 = s.substring( 0, 1);
  22. // 获取除了第一个字符以外的字符
  23. String s2 = s.substring( 1);
  24. // 把A转成大写
  25. String s3 = s1.toUpperCase();
  26. // 把B转成小写
  27. String s4 = s2.toLowerCase();
  28. // C拼接D
  29. String s5 = s3.concat(s4);
  30. System.out.println(s5);
  31. // 优化后的代码
  32. // 链式编程
  33. String result = s.substring( 0, 1).toUpperCase()
  34. .concat(s.substring( 1).toLowerCase());
  35. System.out.println(result);
  36. }
  37. }

String类的其他功能:

 * 
 * 替换功能:
 * String replace(char old,char new)
 * String replace(String old,String new)
 *
 * 去除字符串两空格
 * String trim()
 * 
 * 按字典顺序比较两个字符串  
 * int compareTo(String str)
 * int compareToIgnoreCase(String str)

   
   
  1. package String;
  2. /*
  3. * String类的其他功能:
  4. *
  5. * 替换功能:
  6. * String replace(char old,char new)
  7. * String replace(String old,String new)
  8. *
  9. * 去除字符串两空格
  10. * String trim()
  11. *
  12. * 按字典顺序比较两个字符串
  13. * int compareTo(String str)
  14. * int compareToIgnoreCase(String str)
  15. */
  16. public class Demo10 {
  17. public static void main(String[] args) {
  18. // 替换功能
  19. String s1 = "helloworld";
  20. String s2 = s1.replace( 'l', 'k');
  21. String s3 = s1.replace( "owo", "ak47");
  22. System.out.println( "s1:" + s1);
  23. System.out.println( "s2:" + s2);
  24. System.out.println( "s3:" + s3);
  25. System.out.println( "---------------");
  26. // 去除字符串两空格
  27. String s4 = " hello world ";
  28. String s5 = s4.trim();
  29. System.out.println( "s4:" + s4 + "---");
  30. System.out.println( "s5:" + s5 + "---");
  31. // 按字典顺序比较两个字符串
  32. String s6 = "hello";
  33. String s7 = "hello";
  34. String s8 = "abc";
  35. String s9 = "xyz";
  36. System.out.println(s6.compareTo(s7)); // 0
  37. System.out.println(s6.compareTo(s8)); // 7
  38. System.out.println(s6.compareTo(s9)); // -16
  39. }
  40. }

需求:把数组中的数据按照指定个格式拼接成一个字符串

 * 举例:
 * int[] arr = {1,2,3};
 * 输出结果:
 * "[1, 2, 3]"
 * 分析:
 * A:定义一个字符串对象,只不过内容为空
 * B:先把字符串拼接一个"["
 * C:遍历int数组,得到每一个元素
 * D:先判断该元素是否为最后一个
 * 是:就直接拼接元素和"]"
 * 不是:就拼接元素和逗号以及空格
 * E:输出拼接后的字符串

   
   
  1. package String;
  2. /*
  3. * 需求:把数组中的数据按照指定个格式拼接成一个字符串
  4. * 举例:
  5. * int[] arr = {1,2,3};
  6. * 输出结果:
  7. * "[1, 2, 3]"
  8. * 分析:
  9. * A:定义一个字符串对象,只不过内容为空
  10. * B:先把字符串拼接一个"["
  11. * C:遍历int数组,得到每一个元素
  12. * D:先判断该元素是否为最后一个
  13. * 是:就直接拼接元素和"]"
  14. * 不是:就拼接元素和逗号以及空格
  15. * E:输出拼接后的字符串
  16. */
  17. public class demo11 {
  18. public static void main(String[] args) {
  19. // 前提是数组已经存在
  20. int[] arr = { 1, 2, 3 };
  21. // 定义一个字符串对象,只不过内容为空
  22. String s = "";
  23. // 先把字符串拼接一个"["
  24. s += "[";
  25. // 遍历int数组,得到每一个元素
  26. for ( int x = 0; x < arr.length; x++) {
  27. // 先判断该元素是否为最后一个
  28. if (x == arr.length - 1) {
  29. // 就直接拼接元素和"]"
  30. s += arr[x];
  31. s += "]";
  32. } else {
  33. // 就拼接元素和逗号以及空格
  34. s += arr[x];
  35. s += ", ";
  36. }
  37. }
  38. // 输出拼接后的字符串
  39. System.out.println( "最终的字符串是:" + s);
  40. }
  41. }

字符串反转

 * 举例:键盘录入”abc”
 * 输出结果:” cba
 * 
 * 分析:
 * A:键盘录入一个字符串
 * B:定义一个新字符串
 * C:倒着遍历字符串,得到每一个字符
 * a:length()和charAt()结合
 * b:把字符串转成字符数组
 * D:用新字符串把每一个字符拼接起来
 * E:输出新串

   
   
  1. package String;
  2. /*
  3. * 字符串反转
  4. * 举例:键盘录入”abc”
  5. * 输出结果:”cba”
  6. *
  7. * 分析:
  8. * A:键盘录入一个字符串
  9. * B:定义一个新字符串
  10. * C:倒着遍历字符串,得到每一个字符
  11. * a:length()和charAt()结合
  12. * b:把字符串转成字符数组
  13. * D:用新字符串把每一个字符拼接起来
  14. * E:输出新串
  15. */
  16. public class Demo12 {
  17. public static void main(String[] args) {
  18. // 键盘录入一个字符串
  19. Scanner sc = new Scanner(System.in);
  20. System.out.println( "请输入一个字符串:");
  21. String line = sc.nextLine();
  22. /*
  23. // 定义一个新字符串
  24. String result = "";
  25. // 把字符串转成字符数组
  26. char[] chs = line.toCharArray();
  27. // 倒着遍历字符串,得到每一个字符
  28. for (int x = chs.length - 1; x >= 0; x--) {
  29. // 用新字符串把每一个字符拼接起来
  30. result += chs[x];
  31. }
  32. // 输出新串
  33. System.out.println("反转后的结果是:" + result);
  34. */
  35. // 改进为功能实现
  36. String s = myReverse(line);
  37. System.out.println( "实现功能后的结果是:" + s);
  38. }
  39. /*
  40. * 两个明确: 返回值类型:String 参数列表:String
  41. */
  42. public static String myReverse(String s) {
  43. // 定义一个新字符串
  44. String result = "";
  45. // 把字符串转成字符数组
  46. char[] chs = s.toCharArray();
  47. // 倒着遍历字符串,得到每一个字符
  48. for ( int x = chs.length - 1; x >= 0; x--) {
  49. // 用新字符串把每一个字符拼接起来
  50. result += chs[x];
  51. }
  52. return result;
  53. }
  54. }

统计大串中小串出现的次数


  
  
  1. package String;
  2. /*
  3. * 统计大串中小串出现的次数
  4. * 举例:
  5. * 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
  6. * 结果:
  7. * java出现了5次
  8. *
  9. * 分析:
  10. * 前提:是已经知道了大串和小串。
  11. *
  12. * A:定义一个统计变量,初始化值是0
  13. * B:先在大串中查找一次小串第一次出现的位置
  14. * a:索引是-1,说明不存在了,就返回统计变量
  15. * b:索引不是-1,说明存在,统计变量++
  16. * C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
  17. * D:回到B
  18. */
  19. public class Demo13 {
  20. public static void main(String[] args) {
  21. // 定义大串
  22. String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
  23. // 定义小串
  24. String minString = "java";
  25. // 写功能实现
  26. int count = getCount(maxString, minString);
  27. System.out.println( "Java在大串中出现了:" + count + "次");
  28. }
  29. /*
  30. * 两个明确: 返回值类型:int 参数列表:两个字符串
  31. */
  32. public static int getCount(String maxString, String minString) {
  33. // 定义一个统计变量,初始化值是0
  34. int count = 0;
  35. /*
  36. // 先在大串中查找一次小串第一次出现的位置
  37. int index = maxString.indexOf(minString);
  38. // 索引不是-1,说明存在,统计变量++
  39. while (index != -1) {
  40. count++;
  41. // 把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
  42. // int startIndex = index + minString.length();
  43. // maxString = maxString.substring(startIndex);
  44. maxString = maxString.substring(index + minString.length());
  45. // 继续查
  46. index = maxString.indexOf(minString);
  47. }
  48. */
  49. int index;
  50. //先查,赋值,判断
  51. while((index=maxString.indexOf(minString))!=- 1){
  52. count++;
  53. maxString = maxString.substring(index + minString.length());
  54. }
  55. return count;
  56. }
  57. }

猜你喜欢

转载自blog.csdn.net/weixin_43721133/article/details/88320414