【云计算】API——String等类
小标 2019-02-20 来源 : 阅读 984 评论 0

摘要:本文主要向大家介绍了【云计算】API——String等类,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】API——String等类,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

【云计算】API——String等类

  区别: String: 不可变类 表示一个字符串使用,简单 如果字符串的值频繁的修改,那么不用String,效率低 使用StringBuffer和StringBuilder 多线程使用时,StringBuffer线程是安全的 StringBuilder线程非安全,但速度快 聚集(聚合): 强关联: 整体 和 局部 局部离开整体 可以独立存在。 组合:强聚集 。 整体 和局部。 局部离开整体不可以独立存在。

String类:


package day10;
public class TestString1 {
 public static void main(String[] args) {
  //String 是不可变类: 对象一旦创建了 ,对象的值就不能更改
  //1.         对象
  String s1 = "hello";
  String s2 = new String("Hello");
  String s3 = new String("abc");
  String s4 = "hello";
  String s5 = new String("hello");
  
  // == 
  System.out.println(s1 == s4);
  System.out.println(s1 == s2);
  System.out.println(s2 == s5);
  
  //equals 区分大小写
  System.out.println(s2.equals(s5));
  //
  s1 = "hello" +  "tom";
  System.out.println(s1);//hellotom
  //连接字符串
     s1 = s1.concat("abcdefg");
  System.out.println(s1);
  //-------------------------------------------------
  String s = "hello";
  //长度 (字符的个数)
  System.out.println(s);
  System.out.println(s.length());
  //区分大小 
  System.out.println(s.equals("Hello"));//false
  //  不区分大小写 的"YES"  Yes  yes yES 
  System.out.println(s.equalsIgnoreCase("Hello"));
  //转换 大写 
  System.out.println(s.toUpperCase());
  //转换 小写
  System.out.println(s.toLowerCase());
  //  Demo.java
  // 01234
  // "hello"
  s = "hellohello";
  System.out.println(s);
  // 查找 参数 字符串 在 原始 字符串中 第一次 出现的位置索引
  // 不存在 返回  -1
  System.out.println(s.indexOf("eabc"));//1
  // 查找 参数 字符串 在 原始 字符串中 最后 一次出现的位置索引
  System.out.println(s.lastIndexOf("ell"));//6
  //superman().next().charAt(0)
  
  s = "hellotom";
  System.out.println(s);
  //获得 参数指定位置索引处 的字符  char    
  System.out.println(s.charAt(5));// 't '
  //取 子串 
  //   从 起始位置 取 到 字符串末尾
  System.out.println(s.substring(5));
  //  [起始位置,终止位置)  取到 终止位置的前一位
  System.out.println(s.substring(2, 4));
  //trim 
  s = "  h  e  l l  o     ";
  System.out.println(s);
  //去掉 字符串 前后 空格
  System.out.println(s.trim());
  //
  s = "hellotomhello";
  //字符串替换: 用第二个参数串 替换 第一个 参数串
  System.out.println(s.replace("tom", "汤姆"));
  s = "  h  e  l l  o     ";
  //去除  所有空格
  System.out.println(s.replace(" ", ""));
  //-------------------------------------------
  String str1 = "hello";
  String str2 = "";//空串
  String str3 = null;//空 ,空对象
  
  //---------------------------------------------
  // Demo.java
  
  s = "Demo.java";
  //是否 以指定的参数 为结尾 ,是 true
  System.out.println(s.endsWith("java"));
  //是否 以指定的参数 为开头 ,是 true
  System.out.println(s.startsWith("De"));
  
  //
  String st1 = "abc";
  String st2 = "xyz";
  String st3 = "abc";
  // 比较的对象  比  参数对象 小 (ascII前边) ,返回负数
  System.out.println(st1.compareTo(st2));
  //比较的对象  比  参数对象 大 (ascII后边) ,返回正数
  System.out.println(st2.compareTo(st1));
  //相等 就是 0
  System.out.println(st1.compareTo(st3));
  
  //
  s = "hello";
  System.out.println(s);
  //把字符串 转换成  字符数组    char  []  
  char [] crs = s.toCharArray();
  for(char c : crs) {
   System.out.println(c);
  }
  // String []
  // "aa bb cc dd ee ff"
  s = "aa bb cc";
  //用参数字符串分割 为 一个 字符串 数组   
  String [] strs = s.split(" ");
  for(String ss : strs) {
   System.out.println(ss);
  }
  //指定 参数的 字符串 在 原 字符串 中 是否存在 ,存在 true
  System.out.println(s.contains("aa"));
 }

}


StringBuffer类:


package day10;
public class TestStringBuffer {
 public static void main(String[] args) {
  // StringBuffer 
  StringBuilder sr = new StringBuilder();
  //默认 16个 字符大小缓冲区
  StringBuffer sf1 = new StringBuffer(100);
  
  System.out.println(sf1);
  System.out.println(sf1.capacity());
  sf1.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  System.out.println(sf1.capacity());
  //
 /* StringBuffer sf2 = new StringBuffer("hello");
  System.out.println(sf2.capacity());
  sf2.trimToSize();
  System.out.println(sf2.capacity());*/
  //---------------------------------------------------
  //可变字符串类
  StringBuffer sf = new StringBuffer("hello");
  System.out.println(sf);
  //添加
  System.out.println(sf.append("tom"));
  char [] crs = {'a','b','c'};
  System.out.println(sf.append(crs));//hellotomabc
  //                         (字符数组,从起始位置,个数)
  System.out.println(sf.append(crs, 1, 2));//hellotomabcbc
  //插入一个字符串
  sf.insert(5, "你好");
  System.out.println(sf);//hello你好tomabcbc
  //修改 (位置 ,字符)
  sf.setCharAt(5, '您');
  System.out.println(sf);//hello您好tomabcbc
  //删除
  sf.deleteCharAt(5);//hello好tomabcbc
  System.out.println(sf);
  //  (起始位置,终止位置)  不包括终止位置
  sf.delete(6, 9);//hello好abcbc
  System.out.println(sf);
  //反转
  sf.reverse();//cbcba好olleh
  System.out.println(sf);   
 }
}


          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved