Java学习-String类

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

String 类
1、字符串是一个特殊的对象
2、字符串一旦被初始化就不可更改
3、String s1 = “abc”;
   String s2 = new String(“abc”);
   两者的区别在于S1在内存中有一个对象,S2在内存中有两个对象


4、String类的equal是方法复写了Object类中的equals方法,该方法用于判断字符串是否相





String类常见的操作:


1、获取
1.1 字符串中包含的字符数 int length();
1.2 根据位置获取该位置上的某个字符 char charAt(int index);
1.3 根据字符获取该字符在字符串中的位置 
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
indexOf(String str) 
        返回指定子字符串在此字符串中第一次出现处的索引。
lastIndexOf(int ch) 
        返回指定字符在此字符串中最后一次出现处的索引。


2、判断
2.1 字符串中是否包含某一个字串
boolean contains(CharSequence s) 
        当且仅当此字符串包含指定的 char 值序列时,返回 true。


2.2 字符中是否有内容
boolean isEmpty() 
         当且仅当 length() 为 0 时返回 true。


2.3 字符串是否以指定内容开头
boolean startsWith(String prefix) 
         测试此字符串是否以指定的前缀开始。


2.4 字符串是否以指定内容结尾
boolean endsWith(String suffix) 
        测试此字符串是否以指定的后缀结束。


2.5 判断字符串是否相同
boolean equals(Object anObject) 
        将此字符串与指定的对象比较。


2.6 忽略大小写,判断两个字符串是否相等
boolean equalsIgnoreCase(String anotherString) 
        将此 String 与另一个 String 比较,不考虑大小写。


3、转化
3.1 将字符数组转换为字符串
构造函数:
String(char[] value) 
分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。


String(char[] value, int offset, int count) 
        分配一个新的 String,它包含取自字符数组参数一个子数组的字符。


一般方法:
static String copyValueOf(char[] data) 
          返回指定数组中表示该字符序列的 String。 
static String copyValueOf(char[] data, int offset, int count) 
          返回指定数组中表示该字符序列的 String。 


3.2 将字符串转换为字符数组
char[] toCharArray() 
        将此字符串转换为一个新的字符数组。 
3.3 将字节数组转化为字符串
String(byte[] bytes) 
        通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。   
String(byte[] bytes, int offset, int length) 
        通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。 
String(byte[] bytes, int offset, int length, Charset charset) 
        通过使用指定的 charset 解码指定的 byte 子数组,构造一个新的 String。 


3.4将字符串转换为字节数组
  byte[] getBytes() 
        使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 


byte 数组中。 


3.5 将各中基本数据类型转换为字符串


static String valueOf(boolean b) 
          返回 boolean 参数的字符串表示形式。 
static String valueOf(char c) 
          返回 char 参数的字符串表示形式。 
static String valueOf(double d) 
          返回 double 参数的字符串表示形式。 
static String valueOf(float f) 
          返回 float 参数的字符串表示形式。 
static String valueOf(int i) 
          返回 int 参数的字符串表示形式。 
static String valueOf(long l) 
          返回 long 参数的字符串表示形式。 


4、字符串的替换


String replace(char oldChar, char newChar) 
          返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 


oldChar 得到的。 


5、字符串的切割


String[] split(String regex) 
          根据给定正则表达式的匹配拆分此字符串。


6、字符串中字串的获取
 String substring(int beginIndex) 
          返回一个新的字符串,它是此字符串的一个子字符串。 
 String substring(int beginIndex, int endIndex) 
          返回一个新字符串,它是此字符串的一个子字符串。 


7、转换,去除空格,比较


7.1 大小写转换


String toUpperCase() 
          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 


String toLowerCase(Locale locale) 
          使用给定 Locale 的规则将此 String 中的所有字符都转换为小写 


7.2 去除空格
 String trim() 
          返回字符串的副本,忽略前导空白和尾部空白。 


7.3 比较
 int compareTo(String anotherString) 
          按字典顺序比较两个字符串。 
 int compareToIgnoreCase(String str) 
          按字典顺序比较两个字符串,不考虑大小写。 


StringBuffer


1.字符串的组成原理就是通过该类实现的
2.StringBuffer可以对字符串内容进行增删
3.StringBuffer是一个容器
4.很多方法与String相同
5.StringBuffer是可变长度的


StringBuffer作为一个容器,有以下功能:
1、存储
StringBuffer append():将指定数据作为参数添加到已有数据结尾处
StringBuffer insert(index,数据):将指定数据插入到指定位置index处


2、删除


 StringBuffer delete(int start, int end) 
         删除缓冲区中内容,包含头不包含尾 
 StringBuffer deleteCharAt(int index) 
          删除指定位置的字符
3、获取
 char charAt(int index) 
          返回索引位置的字符
  int indexOf(String str) 
          Returns the index within this string of the first occurrence of the 


specified substring. 
 int indexOf(String str, int fromIndex) 
          Returns the index within this string of the first occurrence of the 


specified substring, starting at the specified index. 


4、修改
 StringBuffer replace(int start, int end, String str) 
          将指定区间的内容修改为str
setCharAt(int index, char ch) 
          将指定位置的字符修改为ch


5、反转
StringBuffer reverse() 
          将字符串反转




StringBulider与StringBuffer功能接近,区别在于:StringBulider是线程不同步的,


StringBuffer是线程同步的。




开发中建议使用StringBulider

















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值