黑马程序员_常用API_String、Stringbuffer、StringBuilder、其它类

----------------------  ASP.Net+Unity开发 .Net培训 、期待与您交流! ----------------------
String类


String是一个特殊的对象,一旦被初始化,就不会被改变。()指的是abc 不是变量 s1.


String s1="abc"; s1是一个类类型变量,“abc”是一个对象。
String s2=new String("abc");


s1和s2的区别:
s1在字符串常量池中创建了一个abc字符串
s2在堆中创建了两个对象一个是默认对象一个是字符串对象。


==和equals的区别


==比较的是地址,equals比较的是内容。


1.获取 
   1.1 获取字符串长度
     int length();
   1.2 根据位置获取字符
     char charAt(int index);
   1.3 根据字符获取在字符中的位置
      int indexof(int ch) 返回的是ch在字符串中第一个出现的位置
      int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
      int indexof(String str); 返回的是str在字符串中第一个出现的位置
      int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
      反响索引一个字符出现的位置。
      int lastindexof(int ch) 返回的是ch在字符串中第一个出现的位置
      int lastindexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
      int lastindexof(String str); 返回的是str在字符串中第一个出现的位置
      int lastindexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置


    1.4获取字符串中的一部分字符串,也叫子串。  
      String subString(int beginindex,intendindex) ;
      String subString(int beginindex) 


2.判断
   2.1两个字符串是否相同
      equals(Object obj)
      equalsIgnoreCase(String str)
   2.2字符串中是否包含某个字符串
      contains(String str)
   2.3两个字符串是否以指定字符串开头或结尾
      boolean Startswith(String);
      boolean endswith(String);
   2.4字符串是否为空
      boolean isEmpty();  


3.字符串转换
   
   3.1将字符串变成字符串数组
       String[] split(String regex);
   3.2将字符串变成字符数组
      char[] toCharArray();
   3.1将字符串变成字节数组
      byte[] getBytes();
   3.4将字符串数组变成字符串
      构造函数 String(char[])
              String(char[],offset,count)将字符数组中的一部分转成字符串。
       静态函数 static String copyValueof(char[])
                static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。
   3.5将字符串的字母大小写转换
       String toUppercase();大写
       String toUppercase();小写
   3.6将字符串的内容替换
       String repalce(char oldch,char newch);
       String repalce(String s1,String s2);
   3.7将字符串两端空格去掉
       String trim();
   3.8将字符串进行连接
       String concat(String);



4.比较 

 compareTo();小返回负数 等返回0 大返回正数

StringBuffer
StringBuffer是个字符串缓冲区对象,用于存储数据的容器
特点:
1.长度是可变的
2.可以存储不同类型数据
3.最终要转换成字符串使用
4.可以对字符串进行修改

功能 
1.添加
  append(data)将指定数据添加到已有数据的结尾处
  insert(位置,字符串) 将数据插入到指定位置
2.删除
  delete(start,end)  删除缓冲区中的数据,包含start不包含end
  deleteCharAt(ine index)删除指定位置元素
  StringBuffer.delete(0,sb.length());
3.查找
  char charAt(index);
  int indexof(string)
  int lastindexof(String);
4.修改 
  StringBuffer  replace(start,end.String)
  void           setCharAt(index,char)
 
  setlength()设置长度
5.反转
  StringBuffer reverse();

6.将缓冲区中的指定数据存储到字符数组中
void getChars(int srcBegin,int srcEnd,char[] dst,int dstbegin)

StringBuilder 类: 不保证线程同步,即不保证安全,而StringBuffer保证线程同步,保证安全。
所以:stringbuffer多用于多线程;stringbuilder多用于单线程。效率高;
其它类
System:描述系统的一些信息

preperties();获取系统信息
Properties prop =new System.getProperties();
是hashtable 的子类。用map的方法去除该类集合中的元素。该集合中存储的都是字符串,没有泛型定义。
String calue=(String)prop.get(obj);
System.out.println(obj+":"+value);

//如何在系统中自定义一些特有信息?
System.setProperty("mykey","myvalue");

Runtime类:使用了单例设计模式。
         
        static Runtime getRuntime();
process p= r.exec("*.exe")//执行文件
  p.destory();杀掉进程。

Date类 
       获取当前时间 
        Date date=new DAte();
       将模式封装到SimpleDateformat对象中 自定义风格
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日  hh:mm:ss");
        调用format方法让模式格式化指定FAte对象
        String time=sdt.format(d);
      

Calendar类

Calendar c=Calendar.getInstance();
c.get(Calendar.Year);//获取年
c.get(Calendar.Month);//获取月
c.get(Calendar.DayofMonth);//获取日
c.get(Calendar.DayofWeek);//获取星期
更改日期
c.add(Calendar.字段,12)
1.获取任意年的二月
c.set(year,2,1)//获取默认一年的三月1日
c.add(Calendar.Day_of_Month,-1)

Math-Random类

cell();返回大雨参数的最小整数
floor()返回小雨参数的最大整数
round()返回四舍五入
pow(a,b)a 的b次方
random();大于等于0.0且小于1.0的伪随机double值

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值