Java入门教程四(字符串处理)

Java 语言的文本数据被保存为字符或字符串类型。字符及字符串的操做主要用到 String 类和 StringBuffer 类,如链接、修改、替换、比较和查找等。php

定义字符串

直接定义字符串

直接定义字符串是指使用双引号表示字符串中的内容,例如"Hello Java"、"Java 编程"等java

String helloWorld ="Hello World";

使用 String 类定义

在 Java 中每一个双引号定义的字符串都是一个 String 类的对象。所以,能够经过使用 String 类的构造方法来建立字符串,该类位于 java.lang 包中。正则表达式

String helloWorld =new String("Hello World");

字符串的链接

经过字符串链接,能够将两个或多个字符串、字符、整数和浮点数等类型的数据连成一个更大的字符串。编程

使用链接运算符

“+”运算符是最简单、最快捷,也是使用最多的字符串链接方式。在使用“+”运算符链接字符串和 int 型(或 double 型)数据时,“+”将 int(或 double)型数据自动转换成 String 类型。数组

int i=10;
String helloWorld="Hello World 第"+i+"次出如今文章中";

使用 concat() 方法

String 类的 concat() 方法实现了将一个字符串链接到另外一个字符串的后面。app

String hello = "Hello";
String world = "World";
String helloWorld = hello.concat(world);

获取字符串长度

使用 String 类的 length() 方法code

String hello = "Hello World";
System.out.println(hello.length());//输出11

转换大小写

toLowerCase() 方法能够将字符串中的全部字符所有转换成小写,toUpperCase() 则将字符串中的全部字符所有转换成大写,非字母的字符不受影响。对象

String helloWorld="Hello World";
System.out.println(helloWorld.toLowerCase());    //输出:helloworld
System.out.println(helloWorld.toUpperCase());    //输出:HELLOWORLD

去除空格

使用 String 类提供的 trim() 方法去掉首尾空格索引

String hello=" hello ";
System.out.println(hello.trim());//去掉首尾空格后hello

字符串截取

String 类的 substring() 方法用于对字符串进行提取,该方法主要有两种重载形式。字符串

substring(int beginIndex)

从索引位置开始至结尾处的字符串部分。调用时,括号中是须要提取字符串的开始位置,方法的返回值是提取的字符串。

String helloWorld="Hello World";
String world=helloWorld.substring(6);
System.out.println(world);    //输出:World

substring(int beginIndex,int endIndex)

beginIndex 表示截取的起始索引,截取的字符串中包括起始索引对应的字符;endIndex 表示结束索引,截取的字符串中不包括结束索引对应的字符,对于开始位置 beginIndex, Java 是基于字符串的首字符索引为 0 处理的,可是对于结束位置 endIndex,Java 是基于字符串的首字符索引为 1 来处理的

String helloWorld="Hello World"; 
System.out.println(helloWorld.substring(2,10));//输出 llo Worl

分割字符串

String 类的 split() 方法能够按指定的分割符对目标字符串进行分割,分割后的内容存放在字符串数组中。str.split(String sign)与str.split(String sign,int limit),str 为须要分割的目标字符串;sign 为指定的分割符,能够是任意字符串;limit 表示分割后生成的字符串的限制个数,若是不指定,则表示不限制,直到将整个目标字符串彻底分割为止。

String color="Red,Black,White,Yellow";
String[] arr1=color.split(",");//不限制元素个数
//arr1为
//Red
//Black
//White
//Yellow
String[] arr2=Colors.split(",",3);    //限制元素个数为3
//arr2为
//Red
//Black
//White,Yellow

字符串的替换

String 类提供了 3 种字符串替换方法,分别是 replace()、replaceFirst() 和 replaceAll()

replace()

replace(String oldChar, String newChar) 方法用于将目标字符串中的指定字符(串)替换成新的字符(串)

String helloWorld="Hello Java";
System.out.println(words.replace("Java","World"));//输出Hello World

replaceFirst()

replaceFirst(String regex, String replacement) 方法用于将目标字符串中匹配某正则表达式的第一个子字符串替换成新的字符串

String words="hello java,hello php";
System.out.println(words.replaceFirst("hello","你好 "));    //输出:你好 java,hello php

replaceAll()

replaceAll(String regex, String replacement) 方法用于将目标字符串中匹配某正则表达式的全部子字符串替换成新的字符串

String words="hello java,hello php";
System.out.println(words.replaceAll("hello","你好 "));    //输出:你好 java,你好 php

字符串的比较

比较字符串的经常使用方法有 3 个:equals() 方法、equalsIgnoreCase() 方法、 compareTo() 方法

equals()

equals() 方法将逐个地比较两个字符串的每一个字符是否相同。对于字符的大小写,也在检查的范围以内。

String a="a";
String b="b";
System.out.println(a.equals(b));    //输出 false

equalsIgnoreCase()

equalsIgnoreCase() 方法的做用和语法与 equals() 方法彻底相同,惟一不一样的是 equalsIgnoreCase() 比较时不区分大小写

String a="ab";
String b="AB";
System.out.println(a.equals(b));    //输出 true

compareTo()

compareTo() 方法用于按字典顺序比较两个字符串的大小,该比较是基于字符串各个字符的 Unicode 值。

String upperA="A";
String lowerA="a";
System.out.println(upperA.compareTo(lowerA));  //输出为-32

查找字符串

字符串查找分为两种形式:一种是在字符串中获取匹配字符(串)的索引值,另外一种是在字符串中获取指定索引位置的字符。分别有三个方法indexOf()、lastlndexOf()和charAt()

indexOf()

indexOf() 方法用于返回字符(串)在指 定字符串中首次出现的索引位置,若是能找到,则返回索引值,不然返回 -1。

str.indexOf(value)
str.indexOf(value,int fromIndex)

str 表示指定字符串;value 表示待查找的字符(串);fromIndex 表示查找时的起始索引,若是不指定 fromIndex,则默认从指定字符串中的开始位置(即 fromIndex 默认为 0)开始查找。

String helloWorld="Hello Java";
int size=s.indexOf('v');    //size的结果为8

lastlndexOf()

lastIndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,若是能找到则返回索引值,不然返回 -1。

str.lastIndexOf(value)
str.lastlndexOf(value, int fromIndex)

lastIndexOf() 方法的查找策略是从右往左查找,若是不指定起始索引,则默认从字符串的末尾开始查找。

String words="today,monday,Sunday";
System.out.println(words.lastIndexOf("day"));//输出16

charAt()

charAt() 方法能够在字符串内根据指定的索引查找字符

String words="today,monday,sunday";
System.out.println(words.charAt(0));    //结果:t

StringBuffer类

除了经过 String 类建立和处理字符串以外,还可使用 StringBuffer 类来处理字符串。StringBuffer 类能够比 String 类更高效地处理字符串。StringBuffer 类是可变字符串类,建立 StringBuffer 类的对象后能够随意修改字符串的内容。每一个 StringBuffer 类的对象都可以存储指定容量的字符串,若是字符串的长度超过了 StringBuffer 类对象的容量,则该对象的容量会自动扩大。

建立 StringBuffer

StringBuffer 类提供了 3 个构造方法来建立一个字符串
StringBuffer() 构造一个空的字符串缓冲区,而且初始化为 16 个字符的容量。
StringBuffer(int length) 建立一个空的字符串缓冲区,而且初始化为指定长度 length 的容量。
StringBuffer(String str) 建立一个字符串缓冲区,并将其内容初始化为指定的字符串内容 str,字符串缓冲区的初始容量为 16 加上字符串 str 的长度。

StringBuffer str1=new StringBuffer();//定义一个空的字符串缓冲区,含有16个字符的容量
StringBuffer str2=new StringBuffer(10);//定义一个含有10个字符容量的字符串缓冲区
StringBuffer str3=new StringBuffer("HelloWorld");//定义一个含有(16+4)的字符串缓冲区,"HelloWorld"为10个字符

追加字符串

StringBuffer 类的 append() 方法用于向原有 StringBuffer 对象中追加字符串。追加内容到当前 StringBuffer 对象的末尾,相似于字符串的链接。

StringBuffer buffer=new StringBuffer("hello,");    //建立一个 StringBuffer 对象
String str="World!";
buffer.append(str);     //向 StringBuffer 对象追加 str 字符串
System.out.println(buffer.substring(0));    //输出:Hello,World!

替换字符

StringBuffer 类的 setCharAt() 方法用于在字符串的指定索引位置替换一个字符。StringBuffer 对象.setCharAt(int index, char ch);

StringBuffer sb=new StringBuffer(“hello");
sb.setCharAt(1,'E');
System.out.println(sb);    //输出:hEllo

反转字符串

StringBuffer 类中的 reverse() 方法用于将字符串序列用其反转的形式取代。StringBuffer 对象.reverse();

StringBuffer sb=new StringBuffer("java");
sb.reverse();
System.out.println(sb);    //输出:avaj

删除字符串

StringBuffer 类提供了 deleteCharAt() 和 delete() 两个删除字符串的方法

deleteCharAt()

deleteCharAt() 方法用于移除序列中指定位置的字符,StringBuffer 对象.deleteCharAt(int index);

StringBuffer sb=new StringBuffer("She");
sb.deleteCharAt(2);
System.out.println(sb);    //输出:Se

delete()

delete() 方法用于移除序列中子字符串的字符,StringBuffer 对象.delete(int start,int end);start 表示要删除字符的起始索引值(包括索引值所对应的字符),end 表示要删除字符串的结束索引值(不包括索引值所对应的字符)。

StringBuffer sb=new StringBuffer("hello jack");
sb.delete(2,5);
System.out.println(sb);    //输出:he jack
相关文章
相关标签/搜索