第五章--字符串的创建方式、连接方式、获取字符串信息的方式、常用操作、格式化、正则表达式、字符串生成器的用法
2017-05-09 17:45
603 查看
1.Sring类
声明字符串变量s: String s;
创建字符串:
(一):String(char a[])方法
用一个字符数组a创建String对象:
提取字符串数组a中的一部分创建一个字符串对象,参数offest表示开始截取字符串的位置,length表示截取字符串的长度。
该构造方法可分配一个新的String对象,使其表示字符数组参数中所有元素连接的结果
(一)连接多个字符串 使用“+”运算符连接字符串
(一)获取字符串长度:
1.indexOf(String s) 该方法用于返回参数字符串s在指定字符串首次出现的索引位置。当调用字符串的indexOf()方法时,会从当前字符串的开始位置搜s的位置;如果没有检索到字符串s,该方法返回值时-1.
2.lastIndexOf(String str) 该方法用于返回指定字符串最后一次出现的索引位置。当调用字符串的lastIndexOf()方法时,会从当前字符串的开始位置检索参数字符串str,并将最后一次出现str的索引位置返回。如果没有检索到,则该方法返回-1.
声明字符串变量s: String s;
创建字符串:
(一):String(char a[])方法
用一个字符数组a创建String对象:
package a; public class test { public static void main(String[] args) { char a[]={'g','o','o','d'};// String s=new String(a);//这两句话等价于:String s=new String("good"); System.out.println(""+s); } } 控制台输出结果: good(二)String(char a[],int offest,int length)
提取字符串数组a中的一部分创建一个字符串对象,参数offest表示开始截取字符串的位置,length表示截取字符串的长度。
package a; public class test { public static void main(String[] args) { char a[]={'s','t','u','d','e','n','t'};// String s=new String(a,2,4);//这两句话等价于:String s=new String("uden"); System.out.println(""+s); } } 控制台输出结果: uden(三)String(char[] value)
该构造方法可分配一个新的String对象,使其表示字符数组参数中所有元素连接的结果
package a; public class test { public static void main(String[] args) { char a[]={'1','2','3','4','5'};// String s=new String(a);//这两句话等价于:String s=new String("12345"); System.out.println(""+s); } } 控制台输出结果: 12345(四)使用字符串常量来创建字符串变量:
package a; public class test { public static void main(String[] args) { String str1,str2; str1="12345"; str2="123456"; System.out.println(""+str1); System.out.println(""+str2); } } 控制台输出结果: 12345 1234562.连接字符串:
(一)连接多个字符串 使用“+”运算符连接字符串
package a; public class test { public static void main(String[] args) { String s1=new String("尾生抱柱"); String s2=new String(","); String s3=new String("至死方休。"); String s=s1+s2+s3; System.out.println(""+s); } } 控制台输出结果: 尾生抱柱,至死方休。(二)连接其他数据类型
package a; public class test { public static void main(String[] args) { int a=4; float b=2.5f; System.out.println("我每天话费"+a+"小时看书"+b+"小时上机练习"); } } 控制台输出结果: 我每天话费4小时看书2.5小时上机练习
System.out.println("我每天话费"+a+"小时看书"+(a+b)+"小时上机练习"); //控制台输出结果:我每天话费4小时看书6.5小时上机练习3.获取字符串信息
(一)获取字符串长度:
package a; public class test { public static void main(String[] args) { String str="123456789"; int size=str.length(); System.out.println(size); } } 控制台输出结果: 9(二)字符串的查找:
1.indexOf(String s) 该方法用于返回参数字符串s在指定字符串首次出现的索引位置。当调用字符串的indexOf()方法时,会从当前字符串的开始位置搜s的位置;如果没有检索到字符串s,该方法返回值时-1.
语法:str.indexOf(substr) str:任意字符串 substr:要搜索的字符串
package a; public class test { public static void main(String[] args) { String str="123456789"; int size=str.indexOf("4"); System.out.println(size); } } 控制台输出结果: 3
2.lastIndexOf(String str) 该方法用于返回指定字符串最后一次出现的索引位置。当调用字符串的lastIndexOf()方法时,会从当前字符串的开始位置检索参数字符串str,并将最后一次出现str的索引位置返回。如果没有检索到,则该方法返回-1.
str.lastIndexOf(substr); str:任意字符串的对象 substr:要搜索的字符串
package a; public class test { public static void main(String[] args) { String str="1234567"; int size=str.lastIndexOf(""); System.out.println("空字符在字符串str中的索引位置是:"+size); System.out.println("字符串str的长度是:"+str.length()); } } 控制台输出结果: 空字符在字符串str中的索引位置是:7 字符串str的长度是:73.获取指定索引位置的字符
语法:str.charAt(int index) str:任意字符串 index:整型值,用于指定要返回字符的下标
package a; public class test { public static void main(String[] args) { String str="qwertyu"; char a=str.charAt(5); System.out.println("字符串str中索引位置是5的字符为:"+a); } } 控制台输出结果: 字符串str中索引位置是5的字符为:y
4.字符串操作 1、获取子字符串 (一)str.substring(int beginIndex); package a; public class test { public static void main(String[] args) { String str="我爱北京天安门!"; String a=str.substring(3); System.out.println(a); } } 控制台输出结果: 京天安门! (二)substring(int beginIndex,endIndex); package a; public class test { public static void main(String[] args) { String str="helloword"; String a=str.substring(0,3);//对字符串进行截取 System.out.println(a); } } 控制台输出结果: hel 2、去除空格 package a; public class test { public static void main(String[] args) { /*String str=" java class"; System.out.println("原来的字符串长度是:"+str.length()); System.out.println("后来的字符串长度是:"+str.trim().length());*/ char[] chars = {'a','b','c','\r','\n'}; System.out.println(chars.length); String str = new String(chars); System.out.println(str.length()); String newStr = str.trim(); System.out.println(newStr.length()); String a="hello word"; System.out.println(a.trim().length()); } } 控制台输出结果: 5 5 3 10 3、字符串替换 package a; public class test { public static void main(String[] args) { String str="abcdefg"; String newstr=str.replace("a","A"); System.out.println(newstr); } } 控制台输出结果: Abcdefg 4、判断字符串的开始与结尾 5、判断字符串是否相等 6、按字典顺序比较两个字符串 7、字母大小比较 8、字符串分割
相关文章推荐
- java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- C#操作字符串以及正则表达式的用法总结
- java---字符串操作(连接,获取信息,比较,替换)
- DataFormatString--格式化字符串 、常用正则表达式
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
- 正则表达式:日期,电话,邮箱等常用字符串;js中日期的带下的比较,获取不同格式的日期
- python 正则表达式获取进程信息文本 ?P 用法
- 使用正则表达式获取连接字符串某项的值
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
- Python通过正则表达式和字符串处理获取方式获取所需子字符串的方式
- java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- Java使用正则表达式及字符串操作,抽取网页信息
- DataFormatString--格式化字符串 、常用正则表达式
- Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化
- 代码笔记 | java 正则表达式高级篇(四种常用的处理方式:匹配 分割 替代 获取)
- 一些字符串操作的常用用法
- 字符串操作(2)---使用正则表达式
- C#中一些字符串操作的常用用法
- C#中一些字符串操作的常用用法