Transcript Document

第五章
字符串
教学内容和目标
掌握字符串的声明与创建
 掌握String类的使用方法
 掌握StringBuffer类的使用方法
 掌握正则表达式的概念和使用

教学重难点
 重点
– Java字符串类的定义和使用
 难点
–
字符串类的各个方法的使用
5.1 字符串

Java语言提供两种字符串类用来实现字符串
操作:
– String类:用于字符串常量,也就是说字符
串的值不会改变
– StringBuffer类:用于字符串变量,也就是
说字符串的值可以改变
String类
Java使用java.lang包中的String类来创建一个
字符串变量,因此字符串变量是对象。
1.字符串常量
如,“你好”,“1234.987”,“weqweo”。
2.声明字符串
String m;
3.创建字符串
使用String类的构造方法,例如:
String s=new String("we are students");

String对象产生方法(一)



由字符串文字直接生成
String str=“Hello everyone”;
生成空串
String str=new String();
复制String对象内容
String oldStr=“Hello”;
String newStr=new String(oldStr);
String对象产生方法(二)
复制StringBuffer对象内容
StringBuffer oldstr=new StringBuffer(“hello”);
String newstr=new String(oldstr);
 复制字符数组内容
char[ ] oldstr={‘h’,’e’,’l’,’l’,’o’};
String newstr=new String(oldstr);

5.2 String字符串的访问操作(一)


length()函数:获取字符串的长度
String mystr=“hello,张三”;
int strlen=mystr.length();
equals函数:比较当前字符串对象的实体是否与参
数指定的字符串实体相同
String tom=new String( "we are students")
String boy=new String( "We are students")
String zgp= new String("we are students")
tom.equals(boy)的值是false
tom.equals(zgp)的值是 true。
5.2 String字符串的访问操作(二)



substring函数:获取字符串的子串
String mystr=“hello,张三”;
substring(int start)
String str1=mystr.substring(6);
Substring(int start,int end)
String str2=mystr.substring(6,8);
String str3=mystr.substring(0,5);
5.2 String字符串的访问操作(三)


indexOf函数:从前向后搜索字符(串)位置
indexOf(int theChar);
indexOf(int theChar,int fromIndex);
indexOf(String str);
indexOf(String str,int fromIndex);
lastIndexOf函数:从后向前搜索字符(串)位置
5.2 String字符串的访问操作(四)


trim()函数:一个字符串s通过调用方法trim()得到一
个字符串对象,该字符串对象是s去掉前后空格后
的字符串。
String mystr=“ hello,张三 ”;
mystr.trim();
charAt()函数:获取串中某个字符
String mystr=“hello,张三”;
char c1=mystr.charAt(0);
char c2=mystr.charAt(7);
5.3 字符串与基本数据的相互转化(一)
 使用java.lang包中的Byte、Short、
Integer 、 Long、Float、Double类
调相应的类方法:
Byte. parseByte(String s)
Short. parseShort(String s)
Long.parseLong(String s)
Float.parseFloat(String s)
Double.parseDouble(String s)
可以将“数字”格式的字符串,
转化为相应的基本数据类型。
5.3 字符串与基本数据的相互转化(二)
 可以将数字转化为字符串的方法:
String str=String.valueOf(byte n)
String str=String.valueOf(int n)
String str=String.valueOf(long n)
String str=String.valueOf(float n)
String str=String.valueOf(double n)
例如:
String s=String.valueOf(123)
5.4 对象的字符串表示

在子类的讲述中我们讲过,所有的类都默认是
java.lang包中Object类的子类或间接子类。
Object类有一个public 方法toString(),一个对
象通过调用该方法可以获得该对象的字符串表
示。
5.5 StringBuffer类


String类创建的字符串对象是不可修改的,也
就是说,String字符串不能修改、删除或替换
字符串中的某个字符,即String对象一旦创建,
那么实体是不可以再发生变化的.
StringBuffer类能创建可修改的字符串序列,
也就是说,该类的对象的实体的内存空间可以
自动的改变大小,便于存放一个可变的字符序
列。
5.5.1 StringBuffer对象产生方法

生成空字符串
StringBuffer myStrBuf=new StringBuffer();

生成指定容量的空字符串
StringBuffer myStrBuf=new StringBuffer(120);

复制String对象中的内容
String str=“hello”;
StringBuffer myStrBuf=new StringBuffer(str);
5.5.2 StringBuffer的容量



字符串长度是指字符串中的字符个数
字符串容量是指StringBuffer对象目前最多
可以容纳的字符个数
如果StringBuffer对象要存放的字符数大于
它的容量(指初始容量),StringBuffer对象
会自动扩大字符串容量,以放下更多的字符
5.5.3 StringBuffer的访问操作






length()函数:获取字符串的长度
charAt()函数:获取串中某个字符
capacity()函数:获取字符串的容量
substring()函数:获取字符串的子串
indexOf()函数:从前向后搜索字符串位置
lastIndexOf():从后向前搜索字符串位置
5.5.4 StringBuffer的修改操作


append函数:在字符串末尾添加新的内容
StringBuffer strBuff=new StringBuffer();
strBuff.append(“hello”);
insert函数:在字符串中的特定位置插入一段子字符串
StringBuffer strBuff=new StringBuffer();
strBuff.append(“helo”);
strBuff.insert(2,”l”);
5.5.5 StringBuffer的修改操作

setCharAt函数:修改指定位置的字符
setCharAt(int index, char ch);

replace函数:修改指定位置的字符串
replace(int start, int end, String str);

deleteCharAt函数:删除指定位置的字符
deleteCharAt(int index);

delete函数:删除指定位置的字符串
delete(int start, int end);
5.6 字符串的连接(一)
“+”:字符串连接符,将不同的字符串、
数字、字符连接成一个完整的字符串
 对于String对象,包括字符串文字,可
以用“+”将两个或多个字符串连接起来,
形成新的字符串。

5.6 字符串的连接(二)

如何区分字符串连接符与加号:
如果以字符串开头,那么其后的“+”号都
作为字符串连接符
如果以数值型数据开头,那么其后的“+”
号都作为加号
5.7 Java Application命令行参数
Java Application是用命令行来启动执行的,
命令行参数就成为向Java Application传入数据的
常用而有效的手段。
Java的命令行参数跟在命令行主类名的后面,参数之
间用空格分隔。
使用命令行参数可以提高程序的灵活性和适应性。不过
在使用命令行参数时要注意数组越界的问题,程序运行时
系统将自动检查用户输入了多少个命令行参数并逐个地保
存在数组args[]中,但是如果程序不检查用户到底输入
了多少个命令行参数而直接访问args[]某下标的数组元
素,则可能造成数组越界异常














public class UseComLParameter
{
public static void main ( String args[ ] )
{
int a1 , a2 , a3 ;
if ( args.length < 2 )
{
System.out.println("运行本程序应该提供两个命令
行参数");
System.exit(0);
}
a1 = Integer.parseInt( args[0] ) ;
a2 = Integer.parseInt( args[1] ) ;
a3 = a1*a2 ;
System.out.println(a1 + " 与 " + a2 + "相乘的积为:"
+ a3 );
数组越界
public class TestSystemException
{
public static void main(String args[])
{
System.out.println(“You have entered
following parameters:”);
for(int i=0; i<3; i++)
System.out.println(″\t″ + args[i]);
}
}
5.8 字符界面的输入输出
所谓字符界面是指字符模式的用户界面。在
字符界面中,用户用字符串向程序发出命令传送数
据,程序运行的结果也用字符的形式表达。虽然图
形用户界面已经非常普及,但是在某些情况下仍然
需要用到字符界面的应用程序,例如字符界面的操
作系统,或者仅仅支持字符界面的终端等。
所有的Java Applet程序都是在图形界面的浏
览器中运行的,所以只有Java Application程序可
以实现字符界面中的输入输出。
单个字符的输入












public class ApplicationCharInOut
{
public static void main(String args[])
{
char c=' ';
System.out.print("Enter a character please: ");
try {
c=(char)System.in.read();
}catch(IOException e){ };
System.out.println("You've entered character "+c);
}
}
字符串的输入















public class ApplicationLineIn
{
public static void main(String args[])
{
String s="";
System.out.print("please enter a string:");
try
{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
}catch(IOException e){ }
System.out.println("You've entered string: " + s);
}
}
上例中使用了java.io包中两个关于输入输出的
类 BufferedReader 和 InputStreamReader 。
System.in代表了系统缺省的标准输入(即键盘),
首先把它转换成InputStreamReader类的对象,然
后转换成BufferedReader类的对象in,使原来的比
特输入变成缓冲字符输入。然后利用readLine( )方
法读取用户从键盘输入的一行字符并赋值给字符串
对象s。
5.9正则表达式


一个正则表达式是含有一些具有特殊意义字
符的字符串,这些特殊字符称作正则表达式
中的元字符。比如,“\\dhello”中的\\d就是
有特殊意义的元字符,代表0到9中的任何一
个。字符串“9hello”和“1hello”都是和正则
表达式:“\\dhello”匹配的字符串之一。
字符串对象调用
public boolean matches(String regex)
方法可以判断当前字符串对象是否和参数
regex指的正则表达式匹配。
作业
 习题5.2
 习题5.3