第五章--字符串的创建方式、连接方式、获取字符串信息的方式、常用操作、格式化、正则表达式、字符串生成器的用法

来源:互联网 发布:qq截图软件绿色版 编辑:程序博客网 时间:2024/05/24 06:33

1.Sring类

声明字符串变量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);}}控制台输出结果:12345123456
2.连接字符串:

(一)连接多个字符串    使用“+”运算符连接字符串

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的长度是:7
3.获取指定索引位置的字符

语法: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);}}控制台输出结果:hel2、去除空格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());}}控制台输出结果:553103、字符串替换package a;public class test {   public static void main(String[] args){String str="abcdefg";String newstr=str.replace("a","A");System.out.println(newstr);}}控制台输出结果:Abcdefg4、判断字符串的开始与结尾5、判断字符串是否相等6、按字典顺序比较两个字符串7、字母大小比较8、字符串分割


0 0
原创粉丝点击