chapter04_Java常用类库.pptx
文本预览下载声明
JAVA 核心技术(中级) ;第四章:;Java 类库;4.1 java.lang包中的常用类 ;例4.1 使用多种方法创建一个字符串并输出字符串内容。;(2)字符串的操作 ;例4.2 比较字符串。; ② 求字符串长度; ③ 连接字符串;可以有四种方法将一个字符串复制到另一个字符数组
或String对象中:copyValueOf、getChars、toCharArray、substring。调用形式:
s1.copyValueOf(data) ----- 将数组data中的内容全部拷贝到字符串中。
s1.copyValueOf(data,int offset,int count) ----- 将数组data中以offset起始,长度为count的内容拷贝到字符串中。
s1.getChars(int strbegin,int strend, data,int offset) ----- 将s1的全部或部分内容拷贝到数组data中。其中,strbegin为字符的起始,strend 为字符的终止,offset为字符数组的起始。
data=s1.toCharArray( ) ----- 将s1中的全部内容拷贝到一个字符数组data中。
s2=s1.substring(int strbegin) ----- 将s1中以stregin起始的内容拷贝到s2中。
s2=s1.substring(int strbegin,int strend) ----- 将s1中以stregin起始,以strend结束之间的内容拷贝到s2中。;
public class StrCopy {
public static void main(String[] args) {
String s1=new String( );
char data[ ]={ a, b, c, d, e, f};
s1=s1.copyValueOf(data);
System.out.println( s1=+s1);
s1=s1.copyValueOf(data,2,3);
System.out.println( s1=+s1);
s1.getChars(1,2, data,0);
System.out.println( data=+data);
data=s1. toCharArray( );
System.out.println( data=+data);
String s2=new String( );
String s3=new String( );
s2=s1.substring(0);
System.out.println( s2=+s2);
s3= s1.substring(1,2);
System.out.println( s3=+s3);
}
};
在字符串中查找字符和子串,确定它们的位置,有几种常用的方法:charAt、indexOf、lastIndexOf。调用形式如下:
s1.chatAt(int index)--------返回s1中index所对应的字符。其中,index是下标号。
s1. indexOf (int char)--------返回s1中字符char在字符串中第一次出现的位置。
s1. lastIndexOf (int char)--------返回s1中字符char在字符串中最后一次出现的位置。
s1. indexOf (s2)--------返回s2在s1中第一次出现的位置。
s1. lastIndexOf (s2)--------返回s2在s1中最后一次出现的位置。 ;public class StrSearch
{ public static void main(String[] args)
{
String s1=Javav;
char c=s1.charAt(2);
System.out.println(c=+c);
int i=s1.indexOf(a);
System.out.println(fistchar=+i);
int j=s1.lastIndexOf(a);
System.out.println(lastchar=+j);
i= s1.indexOf(av);
System.out.println(fiststring=+i);
j=s1.lastIndexOf(av);
System.out.println(laststring=+j);
}}; 修改字符串的常用方法有:replace、toLowerCase、toUpperCase、
显示全部