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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Kevin.Kang 高级黑马   /  2015-7-6 14:20  /  506 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.kxg_04;

  2. public class StringDemo {
  3.         public static void main(String[] args) {
  4.                 String s = "helloworld";
  5.                 String s2 = "HelloWorld";
  6.                 String s3 = "HELLOWORLD";
  7.                 String s4 = "helloworld";
  8.                 String s5 = "hello";
  9.                 String s6 = "";
  10.                 // String s7 = null;

  11.                 // A:判断功能
  12.                 // boolean equals(Object obj):比较字符串内容是否相同,区分大小写
  13.                 System.out.println(s.equals(s4));
  14.                 // boolean equalsIgnoreCase(String str):比较字符串内容是否相同,不区分大小写
  15.                 System.out.println(s.equalsIgnoreCase(s2));
  16.                 // boolean contains(String str):判断大字符串中是否包含小字符串
  17.                 System.out.println(s.contains(s5));
  18.                 // boolean startsWith(String str):判断字符串是否以指定字符串开头
  19.                 System.out.println(s.startsWith(s5));
  20.                 // boolean endsWith(String str):判断字符串是否以指定字符串结尾
  21.                 System.out.println(s.endsWith("ld"));
  22.                 // boolean isEmpty():判断字符串是否为空
  23.                 System.out.println(s6.isEmpty());
  24.                 // System.out.println(s7.isEmpty());//s7对象指向空,空指针异常
  25.                 System.out.println("===================");

  26.                 // B:获取功能
  27.                 // int length():获取字符串长度
  28.                 System.out.println(s.length());
  29.                 // char charAt(int index):获取指定索引位置的字符
  30.                 System.out.println(s.charAt(5));
  31.                 // int indexOf(int ch):获取指定字符在字符串中的索引
  32.                 System.out.println(s.indexOf("l"));
  33.                 // int indexOf(String str):获取定子字符串在此字符串中第一次出现处的索引
  34.                 System.out.println(s.indexOf("l"));
  35.                 // int indexOf(int ch,int fromIndex):获取指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
  36.                 System.out.println(s.indexOf("l", 3));
  37.                 // int indexOf(String str,int
  38.                 // fromIndex):获取指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
  39.                 System.out.println(s.indexOf("l", 3));
  40.                 // String substring(int start):从指定索引截取字符串,默认到结尾
  41.                 System.out.println(s.substring(6));
  42.                 // String substring(int start,int end):从指定位置截取字符串到指定位置结束
  43.                 System.out.println(s.substring(4, 7));
  44.                 System.out.println("===================");

  45.                 // C:转换功能
  46.                 // byte[] getBytes():字符串转换为字节数组
  47.                 byte[] by = s.getBytes();
  48.                 for (int x = 0; x < by.length; x++) {
  49.                         System.out.println(by[x]);
  50.                 }
  51.                 // char[] toCharArray():字符串转换为字符数组
  52.                 char[] str = s.toCharArray();
  53.                 for (int x = 0; x < str.length; x++) {
  54.                         System.out.println(str[x]);
  55.                 }
  56.                 // static String valueOf(char[] chs):字节数组转换为字符串
  57.                 System.out.println(String.valueOf(str));
  58.                 // static String valueOf(int i):int类型的数据转成字符串
  59.                 int i = 48;
  60.                 String str2 = String.valueOf(i);
  61.                 System.out.println(str2);
  62.                 // String toLowerCase():字符串转换为小写
  63.                 System.out.println(s3.toLowerCase());
  64.                 // String toUpperCase():字符串转换为大写
  65.                 System.out.println(s.toUpperCase());
  66.                 // String concat(String str):字符串拼接
  67.                 System.out.println(s.concat(s5));
  68.                 System.out.println("===================");

  69.                 // D:其他功能
  70.                 // a:替换功能
  71.                 // String replace(char old,char new)
  72.                 System.out.println(s.replace("l", "k"));
  73.                 // String replace(String old,String new)
  74.                 System.out.println(s.replace("ll", "kkk"));

  75.                 // b:去空格功能
  76.                 // String trim()
  77.                 String sk = "   helloworld     ";
  78.                 System.out.println(sk.trim());

  79.                 // c:按字典比较功能
  80.                 // int compareTo(String str)
  81.                 System.out.println(s.compareTo(s4));
  82.                 System.out.println(s.compareTo(s5));
  83.                 // int compareToIgnoreCase(String str)

  84.         }
  85. }
复制代码


3 个回复

倒序浏览
顶一下  {:3_57:}
回复 使用道具 举报
  1. package com.kxg_01;

  2. public class StringBufferDemo {
  3.         public static void main(String[] args) {

  4.                 // StringBuffer的构造方法
  5.                 // StringBuffer的方法
  6.                 // public int capacity()返回缓冲区容量
  7.                 // A:StringBuffer():无参构造
  8.                 StringBuffer sb = new StringBuffer();
  9.                 System.out.println(sb);
  10.                 System.out.println(sb.capacity());
  11.                 System.out.println(sb.length());
  12.                 // B:StringBuffer(int size):指定容量的字符串缓冲区对象
  13.                 StringBuffer sb2 = new StringBuffer(50);
  14.                 System.out.println(sb2);
  15.                 System.out.println(sb2.capacity());
  16.                 System.out.println(sb2.length());
  17.                 // C:StringBuffer(String str):指定字符内容的字符串缓冲区内存对象
  18.                 StringBuffer sb3 = new StringBuffer("helloworld");
  19.                 System.out.println(sb3);
  20.                 System.out.println(sb3.capacity());
  21.                 System.out.println(sb3.length());

  22.                 // A:添加功能
  23.                 // public StringBuffer append(String str):把任意类型元素添加到字符串缓冲区中,并返回本身
  24.                 sb.append("sdfsdf").append(12313).append(12.332);
  25.                 System.out.println(sb);
  26.                 // public StringBuffer insert(int offset,String
  27.                 // str):指定位置插入任意类型元素到字符串缓冲区,并返回本身
  28.                 sb.insert(5, "xiao");
  29.                 System.out.println(sb);

  30.                 // B:删除功能
  31.                 // public StringBuffer deleteChatAt(int index):删除指定位置的字符,并返回本身
  32.                 sb.deleteCharAt(6);
  33.                 System.out.println(sb);
  34.                 // public StringBuffer deleteCharAt(int start, int
  35.                 // end):删除指定位置到结束位置,并返回本身
  36.                 sb.delete(6, 12);
  37.                 System.out.println(sb);
  38.                 sb.delete(10, sb.length());
  39.                 System.out.println(sb);

  40.                 // C:替换功能
  41.                 // public StringBuffer replace(int start,int end,String
  42.                 // str):从指定位置到结尾用指定字符串代替
  43.                 sb.replace(2, 5, "xiaoxiaoxiao");
  44.                 System.out.println(sb);

  45.                 // D:反转功能
  46.                 // public StringBuffer reverse():把字符串反转
  47.                 sb.reverse();
  48.                 System.out.println(sb);

  49.                 // E:截取功能
  50.                 // public String substring(int start):指定位置截取,默认到结尾,返回String类型
  51.                 // public String substring(int start,int end):指定位置到指定结尾,返回String类型
  52.                 String s = sb.substring(2);
  53.                 String s2 = sb.substring(2, 5);
  54.                 System.out.println(s);
  55.                 System.out.println(s2);
  56.                 System.out.println(sb);
  57.         }
  58. }
复制代码
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马