下载此文档

String常用的方法介绍.doc


文档分类:IT计算机 | 页数:约11页 举报非法文档有奖
1/11
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/11 下载此文档
文档列表 文档介绍
String 介绍1
一、String类1
二、String类对象的创建1
三:常用方法2
1、求字符串长度2
2、求字符串某一位置字符2
3、提取子串2
4、字符串比较2
5、字符串连接3
6、字符串中单个字符查找3
(str2);//b=0
5 boolean c = (str2);//c=false
6 boolean d = (str2);//d=true
复制代码
5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";
6、字符串中单个字符查找
1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
复制代码
1 String str = "I am a good student";
2 int a = ('a');//a = 2
3 int b = ("good");//b = 7
4 int c = ("w",2);//c = -1
5 int d = ("a");//d = 5
6 int e = ("a",3);//e = 2
复制代码
7、字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
1 String str = new String("asDF");
2 String str1 = ();//str1 = "asdf"
3 String str2 = ();//str2 = "ASDF"
8、字符串中字符的替换
1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
1 String str = "asdzxcasd";
2 String str1 = ('a','g');//str1 = "gsdzxcgsd"
3 String str2 = ("asd","fgh");//str2 = "fghzxcfgh"
4 String str3 = ("asd","fgh");//str3 = "fghzxcasd"
5 String str4 = ("asd","fgh");//str4 = "fghzxcfgh"
9、截去字符串两端的空格
1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。

String常用的方法介绍 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数11
  • 收藏数0 收藏
  • 顶次数0
  • 上传人taoapp
  • 文件大小44 KB
  • 时间2022-01-25
最近更新