A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© Synaric 中级黑马   /  2015-10-5 11:05  /  286 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //演示创建、获取操作
  2. public class StringDemo {
  3.         public static void main(String[] args) {
  4.                 //创建字符串
  5.                 String s1 = new String("ABCDEF");
  6.                 String s2 = "EFGHI";
  7.                
  8.                 //获取字符串的长度
  9.                 int length = s1.length();
  10.                 System.out.println(length);
  11.                
  12.                 //根据index获取指定位置的char
  13.                 char c = s1.charAt(0);
  14.                 System.out.println(c);
  15.                
  16.                 //返回'C'在字符串中第一次出现的位置,没有找到返回-1
  17.                 int p0 = s1.indexOf('C');
  18.                 int p1 = s1.indexOf('J');
  19.                 System.out.println(p0);
  20.                 System.out.println(p1);
  21.                 System.out.println();
  22.                
  23.                 //从指定位置开始,获取'C'在字符串中第一次出现的位置
  24.                 int p2 = s1.indexOf('C', 2);
  25.                 int p3 = s1.indexOf('C', 3);       
  26.                 System.out.println(p2);
  27.                 System.out.println(p3);
  28.                 System.out.println();
  29.                
  30.                 //返回"CDE"在字符串中第一次出现的位置
  31.                 int p4 = s1.indexOf("CDE");
  32.                 System.out.println(p4);
  33.                
  34.                 //从指定位置开始,获取"CDE"在字符串中第一次出现的位置
  35.                 int p5 = s1.indexOf("CDE", 1);
  36.                 System.out.println(p5);
  37.                
  38.                 //从后向前搜索返回第一次'C'出现的位置
  39.                 //注意返回值仍然是2。反向的是搜索方向,而不是索引
  40.                 int p6 = s1.lastIndexOf('C');
  41.                 System.out.println(p6);
  42.                
  43.                 String s3 = "ABCABCABC";
  44.                 int p7 = s3.lastIndexOf('C');
  45.                 System.out.println(p7);
  46.         }
  47. }
复制代码
  1. //演示判断操作
  2. public class StringDemo2 {
  3.         public static void main(String[] args) {
  4.                 //先创建个字符串
  5.                 String s1 = "This is a String type.";
  6.                
  7.                 //字符串中是否包含子串" is a"
  8.                 boolean b1 = s1.contains(" is a");
  9.                 System.out.println(b1);
  10.                
  11.                 //字符串是否为空
  12.                 boolean b2 = s1.isEmpty();
  13.                 System.out.println(b2);
  14.                
  15.                 //字符串是否以"Thi"开头
  16.                 boolean b3 = s1.startsWith("Thi");
  17.                 System.out.println(b3);
  18.                
  19.                 //字符串是否以"type."结尾
  20.                 Boolean b4 = s1.endsWith("type.");
  21.                 System.out.println(b4);
  22.                
  23.                 //比较Unicode序列是否相同
  24.                 String s2 =  new String("This is a String type.");
  25.                 Boolean b5 = s1.equals(s2);
  26.                 System.out.println(b5);
  27.                
  28.                 //比较Unicode序列是否相同,忽略大小写
  29.                 String s3 =  new String("ThiS is A string tyPe.");
  30.                 Boolean b6 = s1.equalsIgnoreCase(s3);
  31.                 System.out.println(b6);               
  32.         }
  33. }
复制代码
  1. //演示转换操作
  2. public class StringDemo3 {
  3.         public static void main(String[] args) {
  4.                 //将字符串转换为char[]
  5.                 char[] chars = "This is a String type.".toCharArray();
  6.                
  7.                 //构造函数,将char[]转换为字符串
  8.                 String s1 = new String(chars);
  9.                 String s2 = new String(chars, 2, 5);
  10.                 System.out.println(s1);
  11.                 System.out.println(s2);
  12.                 System.out.println();
  13.                
  14.                 //静态方法,将char[]转换为字符串
  15.                 String s3 = String.copyValueOf(chars);
  16.                 String s4 = String.copyValueOf(chars, 3, 4);
  17.                 String s5 = String.valueOf(chars);
  18.                 System.out.println(s3);
  19.                 System.out.println(s4);
  20.                 System.out.println(s5);
  21.                 System.out.println();
  22.                
  23.                 //强字符串转换为byte[]
  24.                 byte[] bytes = "This is a String type.".getBytes();
  25.                 System.out.println(bytes);
  26.                
  27.                 //构造函数,将char[]转换为字符串
  28.                 String s6 = new String(bytes);
  29.                 String s7 = new String(bytes, 3, 4);
  30.                 System.out.println(s6);
  31.                 System.out.println(s7);
  32.                 System.out.println();
  33.                
  34.                 //将基本数据类型转换为字符串
  35.                 String s8 = String.valueOf(1.27);
  36.                 String s9 = String.valueOf(3.14f);
  37.                 System.out.println(s8);
  38.                 System.out.println(s9);
  39.         }
  40. }
复制代码

  1. import java.util.Arrays;

  2. public class StringDemo4 {
  3.         public static void main(String[] args) {
  4.                 //先创建一个字符串
  5.                 String s1 = "abc def abc abcdefgh";
  6.                
  7.                 //替换指定字符'c'
  8.                 String s2 = s1.replace('c', 'm');
  9.                 System.out.println(s2);
  10.                
  11.                 //以" "切割字符串
  12.                 String[] arr = s1.split(" ");
  13.                 System.out.println(Arrays.toString(arr));
  14.                
  15.                 //获取字符串从索引3开始的一部分
  16.                 String s3 = s1.substring(3);
  17.                 System.out.println(s3);
  18.                
  19.                 //获取字符串从索引3开始5结尾(不包括5)的一部分
  20.                 String s4 = s1.substring(3, 5);
  21.                 System.out.println(s4);
  22.                
  23.                 //将字符串转换为大写
  24.                 String s5 = s1.toUpperCase();
  25.                 System.out.println(s5);
  26.                
  27.                 //将字符串转换为小写
  28.                 String s6 = s1.toLowerCase();
  29.                 System.out.println(s6);
  30.                
  31.                 //将字符串两端的多个空格去除
  32.                 String s7 = "  abc     ";
  33.                 String s8 = s7.trim();
  34.                 System.out.println(s8);
  35.                
  36.                 //将两个字符串进行自然顺序比较
  37.                 String s9 = "abc";
  38.                 int x = s1.compareTo(s9);
  39.                 System.out.println(x);
  40.         }
  41. }
复制代码



1 个回复

倒序浏览
学习了      
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马