码迷,mamicode.com
首页 > 其他好文 > 详细

黑马程序员——String类总结

时间:2015-07-18 00:25:09      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 ------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

 

String类总结

String类的方法

         一、String类的构造方法()

String s = new String();

通过字节数组创建字符串对象

String s = new String(byte[] bys); 

通过字节数组创建字符串对象常用

String s = new String(byte[] bys,int index, int length)

通过字节数组一部分创建字符串对象

String s = new String(char[] chs)

通过字符数组创建字符串对象常用

String s = new String(char[] chs, int index, int length);

通过字符数组一部分创建字符串对象

String s = new String(String); 

通过给构造方法传入字符串创建字符字符串对象

String s = “helloworld”

直接给字符串对象赋值常用

 

         二、String类的成员方法(判断,获取,转化,其他)

1、判断功能

boolean equals(Object obj)  判断字符串的内容是否相同,区分大小写

boolean equalsIgnoreCase(String str) 判断字符串的内容是否相同,忽略大小写

boolean contains(String str) 判断字符串对象是否包含给定的字符串

boolean startsWith(String str) 判断字符串对象是否是以给定的字符串开始

boolean endsWith(String str) 判断字符串对象是否是以给定的字符串结束

boolean isEmpty()判断字符串对象是否为空,注意是数据


2、获取功能

int length()获取字符串长度

char charAt(int index) 返回字符串中给定索引处的字符

int indexOf(int ch) 返回指定字符在此字符串中第一次出现的索引

int indexOf(String str) 返回指定字符串在此字符串中第一次出现的索引

int indexOf(int ch,int fromIndex)

 返回指定字符在此字符串中第一次出现的索引,从指定位置开始

int indexOf(String str,int fromIndex)

返回指定字符在此字符串中第一次出现的索引,从指定位置开

String substring(int start) 截取字符串,返回从指定位置开始截取后的字符串,原字符串长度不变

String substring(int start,int end)

截取字符串,返回从指定位置开始到指定位置结束,截取后的字符串原字符串长度不变

//如果start=0,end=该字符串的长度,那么返回的是原对象,否则返回新对象


3、转换功能

byte[] getBytes()把字符串转换成字节数组

char[] toCharArray()把字符串转换成字符数组

static String copyValueOf(char[] chs) 把字符数组转换成字符串

底层调用new String(char[] chs)

static String valueOf(char[] chs) 把字符数组转换成字符串

底层调用new String(char[] chs)

static String valueOf(任意类型 变量名) 把任意类型转换成字符串

String toLowerCase()把字符变成小写原字符串不变

String toUpperCase()把字符编程大写原字符串不变

String concat(String str) 字符串链接原字符串不变

 

4、其他功能

String replace(char oldChar,char newChar) 替换功能原字符串不变

String replace(String oldString,String newString) 替换功能原字符串不变

String[] split(String str) 切割功能原字符串不变字符串的split方法中的字符串

String trim()去除字符串两端空格原字符串长度不变

int compareTo(String str) 字典顺序比较功能

int compareToIgnoreCase(String str) 字典顺序比较功能

以“统计字符串中大写,小写,数字字符出现的次数”为例,将部分方法演示如下:

  技术分享

黑马程序员——String类总结

标签:

原文地址:http://www.cnblogs.com/litie/p/4655926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!