Java String类的简单介绍

String 的特点:
1 String 类代表字符串,所有字符串都作为此类的实例实现
2 字符串是常量;他们的值在创建后不能更改
3 所有常量在内存的方法区的常量池中

常量:
字面值常量:”ABC“
自定义常量:String s = ”ABC“

4 字符串缓冲区【StringBuffer,StringBuilder】支持可变的字符串
5 String 对象是不可变的,所以可以共享
6 Java 在编译时期会针对字符串的拼接操作使用StringBuilder改进,提高效率,节约资源

String s = ”ABC“和String s = new String(”ABC“)的区别:
前者在方法区开辟空间
后者即在方法区,也在堆区开辟空间
一般使用前者创建字符串对象

String类的构造方法:

/*  
 * 
 * public String()空串
 * 
 * public String(String original)普通字符串
 * 
 * public String(char[] value)或者public String(char value[])传入一个字符型的数组
 * 
 * public String(char value[], int offset, int count)从字符数组value索引为offset开始,长度为count
 * 
 * public String(byte bytes[])传入一个byte型数组bytes,数组的内容转化成字符串时,要对应ASCII码表;
 * 
 * public String(byte bytes[], int offset, int length)传入一个byte型数组bytes,索引为offset开始,长度为count
 * 
 * public String(byte bytes[], Charset charset)
 * 
 */

String 类的方法(一):

/*  
 * 
 * char charAt(int index)索引为index处的字符
 * 
 * int indexOf(int ch)字符为ch处的索引
 * 
 * int indexOf(String str)字符串为str处的索引
 * 
 * int indexOf(int ch,int fromIndex)从索引为fromIndex开始,字符为ch处的索引
 * 
 * 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处的最后一个索引
 * 
 * String substring(int statrt)从字符串索引为statrt开始的子串
 * 
 * String substring(int statrt,int end)从字符串索引为statrt开始end结束的子串(左闭右开原则)
 * 
 * int length()字符串长度
 */

String 类的方法(二):

/*  
 *  boolean isEmpty():判断字符串是否为空。
    
 *  boolean equals(Object obj):将此字符串的内容与指定的对象比较,区分大小写。
    
 *  boolean equalsIgnoreCase(String str):将此 String 与另一个 String 比较,忽略大小写。
    
 *  boolean contains(String str):判断字符串中是否包含方法传入的字符串。
    
 *  boolean startsWith(String str):判断字符串是否以某个指定的字符串开头。
    
 *  boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾。
​
 */

String 类的方法(三):

/*  
 *  byte[] getBytes() :将字符串转化为字节数组。
​
 *  char[] toCharArray(): 将字符串转化为字符数组。
​
 *  static String valueOf(char[] chs): 返回 char 数组参数的字符串表示形式。
​
 *  static String valueOf(int i) :返回 int 参数的字符串表示形式。
​
 *  String toLowerCase() :将此 String 中的所有字符都转换为小写。
​
 *  String toUpperCase() :将此 String 中的所有字符都转换为大写。
​
 *  String concat(String str): 将指定字符串连接到此字符串的结尾。
 */

String 类的方法(四):

/*  
 *  String replace(char old,char new) :替换功能
 *       "abc".replace('a', 'b')     "bbc"
        
 *  String replace(String old,String new) :替换功能
 *      "abc".replace("ab", "cd")   "cdc"       注意:替换不会改变原字符串
        
 *  String trim():去除字符串首尾空格
 *      " abc ".trim()              "abc"
        
 *  int compareTo(String str) :按字典顺序比较两个字符串,区别大小写
 *       "ac".compareTo("ab")        1
        
 *  int compareToIgnoreCase(String str):按字典顺序比较两个字符串,忽略大小写
 *       "ac".compareToIgnoreCase("ab")          1
        
 *  public String[] split(String regex):分隔字符串成字符数组
 *      Arrays.toString("ab-cd-ef".split("-"))       [ab, cd, ef]
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值