API(APPlication Programming Interface)指:应用程序编程接口。
String类和StringBuffer类
String类和StringBuffer类
|
String类的初始化
String类的构造方法:
方法声明
|
功能描述
|
String()
|
创建一个内容为空的字符串
|
String(char[] value)
|
根据指定的字符数组创建对象
|
String(String value)
|
根据指定的字符串内容 创建对象
|
String类的常见操作
String常用方法
方法声明
|
功能描述
|
Int indexOf (int ch)
|
返回指定字符在此字符串中第一次出现处的索引
|
Int lastIndexOf (int ch)
|
返回指定字符在此字符串中最后一次出现处的索引
|
Char chrAt (int index)
|
返回字符串中index位置上的字符,其中inde的取值范围是:0~(字符串长度-1)
|
Boolean endsWith (String suffix)
|
判断此字符串是否以指定的字符串结尾
|
Int length ()
|
返回此字符串的长度
|
Boolean equals (Object anObject)
|
将此字符串与指定的字符串比较
|
Boolean isEmpty ()
|
当且仅当字符串长度为0时返回true
|
Boolean startsWith (String prefix)
|
判断此字符串是否以指定的字符串开始
|
Boolean contains (CharSequence cs)
|
判断此字符串中是否包含指定的字符序列
|
String toLowerCase ()
|
使用默认语言环境的规则将String中的所有字符都转换为小写
|
String toUpperCase ()
|
使用默认语言环境的规则将String中的所有字符都转换为大写
|
String valueOf (int i)
|
返回int参数的字符串表示形式
|
Char[] toCharArray ()
|
将此字符串转换为一个字符数组
|
String replace (CharSequence oldstr, CharSequence newstr)
|
返回一个新的字符串,它是通过用newstr替换此字符串中出现的所有oldstr得到的
|
String[] split (String regex)
|
根据参数regex将原来的字符串分割为若干个子字符串
|
String substring (int beginIndex)
|
返回一个新字符串,它包含字符串中索引beginIndex后的所有字符
|
String substring (int beginIndex, int endIndex)
|
返回一个新字符串,它包含此字符串中从索引beginIndex后的所有字符
|
String trim ()
|
返回一个新字符串,它去除了原字符串首尾的空格
|
- 字符串的基本操作
- 字符串的转换操作
- 字符串的替换和去除空格操作
- 字符串的判断操作
- 字符串的截取和分割
StringBuffer类(字符串缓冲区)
StringBuffer和String最大的区别在于它的内容和长度都是可以改变的。
StringBuffer类常用方法
方法声明
|
功能描述
|
StringBuffer append (char c)
|
添加参数到StringBuffer对象中
|
StringBuffer insert (int offset, String str)
|
将字符串中的offset位置插入字符串str
|
StringBuffer deleteCharAt (int index)
|
移除此序列指定位置的字符
|
StringBuffer delete (int start, int end)
|
删除StringBuffer对象中指定范围的字符或字符串序列
|
StringBuffer replace (int start, int end, String s)
|
在StringBuffer对象中替换指定的字符或字符串序列
|
Void setCharAt (int index, char ch)
|
修改指定位置index处的字符序列
|
String toString ()
|
返回StringBuffer缓冲区中的字符串
|
StringBuffer reverse ()
|
将此字符序列用其反转形式取代
|
public class Example08 {
public static void main(String[] args) {
System.out.println("1、添加------------------");
add();
System.out.println("2、删除------------------");
remove();
System.out.println("3、修改------------------");
alter();
}
public static void add() {
//定义一个字符串缓冲区
StringBuffer sb = new StringBuffer();
//在末尾添加字符串
sb.append("abcdefg");
System.out.println("append添加结果:"+sb);
//在指定位置插入字符串
sb.insert(2, "123");
System.out.println("insert添加结果:"+sb);
}
public static void remove() {
//定义一个字符串缓冲区
StringBuffer sb = new StringBuffer("abcdefg");
//指定范围删除
sb.delete(1, 5);
System.out.println("删除指定位置结果:"+sb);
//指定位置删除
sb.deleteCharAt(2);
System.out.println("删除指定位置结果:"+sb);
//清空缓冲区
sb.delete(0, sb.length());
System.out.println("清空缓冲区结果:"+sb);
}
public static void alter() {
StringBuffer sb = new StringBuffer("abcdef");
//修改指定位置字符
sb.setCharAt(1, 'p');
System.out.println("修改指定位置字符结果:"+sb);
//替换指定位置字符串或字符
sb.replace(1, 3, "qq");
System.out.println("替换指定位置字符(串)结果:"+sb);
System.out.println("字符串翻转结果:"+sb.reverse());
}
}
|
StringBuffer和String进行对比,归纳两者的不同。
- String类表示的字符串是常量,一旦创建后,内容和长度都是无法改变的。而StringBuffer表示字符容器其内容和长度都可以随时修改。在操作字符串时,如果该字符串仅用于表示数据类型,则使用String即可,但是如果需要对字符串中的字符进行增删操作,则使用StringBuffer类。
- String类覆盖了Object类的equals()方法,而StringBuffer类没有覆盖Object类的equals()方法。
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2)); //结果为true
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb1.equals(sb2)); //结果为false
|
- String类对象可以用操作符+进行连接,而StringBuffer类对象之间不能
String s1 = new String("a");
String s2 = new String("b");
String s3 = s1+s2;
System.out.println(s3); //结果为ab
StringBuffer sb1 = new StringBuffer("a");
StringBuffer sb2 = new StringBuffer("b");
StringBuffer sb3 = sb1+sb2; //编译出错
|
|
System类与Runtime类
System类与Runtime类
|
System类
System类的常用方法:
方法声明
|
功能描述
|
Start void exit (int status)
|
该方法用于终止当前正在运行的Java虚拟机,其中参数status表示状态码,若状态码非0,则表示异常终止。
|
Static long gc ()
|
运行垃圾回收器,并对垃圾进行回收。
|
Static long currentTimeMills ()
|
返回以毫秒为单位的当前时间。
|
Static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length)
|
从src引用的指定源数组复制到dest引用的数组,复制从指定的位置开始,到目标数组的指定位置结束。
|
Static Properties getProperties ()
|
取得当前的系统属性。
|
Static String getProperty (String key)
|
获取指定键描述的系统属性。
|
- getProperties()方法
getProperties()方法用于获取当前系统的全部属性,该方法会返回一个Properties对象,其中封装了系统的所有属性,这些属性是以键值对的形式存在。
public static void main(String[] args) {
//获取当前系统属性
Properties properties = System.getProperties();
//获得所有系统属性的key,返回Enumeration对象
Enumeration<?> propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
//获取系统属性的键key
String key = (String) propertyNames.nextElement();
//获得当前键key对应的值value
String value = System.getProperty(key);
System.out.println(key+"---"+value);
}
}
|
- currentTimeMillis()
currentTimeMillis()返回一个long类型的值,该值表示当前的时间与1970年1月0点0分0秒之间的时间差,单位是毫秒。被称为时间戳。
public static void main(String[] args) {
//循环开始时的当前时间
long startTime = System.currentTimeMillis();
int sum = 0;
for (int i=0; i<100000000; i++) {
sum += i;
}
//循环结束时的当前时间
long endTime = System.currentTimeMillis();
System.out.println("程序运行的时间为:"+(endTime-startTime)+"毫秒");
}
|
- arraycopy (Object src, int srcPos, Object dest, int destPos, int length)
用于将一个数组中的元素快速拷贝到另一个数组
Src:
|
表示源数组
|
Dest:
|
表示 目标数组
|
srcPos:
|
表示源数组中拷贝元素的起始位置
|
destPos:
|
表示拷贝到目标数组的起始位置
|
Length:
|
表示拷贝元素的个数
|
public static void main(String[] args) {
//源数组
int [] fromArray = {101, 102, 103, 104, 105, 106};
//目标数组
int [] toArray = {201, 202, 203, 204, 205, 206, 207};
/**
* fromArray——————src——————源数组
* 2——————srcPos——————起始位置
* toArray——————dest——————目标数组
* 3——————destPos——————起始位置
* 4——————length——————拷贝个数
*/
//拷贝数组元素
System.arraycopy(fromArray, 2, toArray, 3, 4);
for (int i=0; i<toArray.length; i++) {
System.out.println(i+":"+toArray[i]);
}
}
|
System类两个常见的方法:
Gc():
|
启动Java的垃圾回收器,并对内存中的垃圾对象进行回收
|
Exit(int status)
|
终止当前正在运行的Java虚拟机,其中参数status用于表示当前发生的异常状态。通常指定为0,表示正常退出,否则表示异常终止。
|
Runtime类
Runtime类用于表示虚拟机运行时的状态,它用于封装JVM虚拟机进程。每次使用Java命令启动虚拟机都对应一个Runtime实例,并且只有一个实例,因此该类采用单例模式进行设计,对象不可以直接实例化,
想在程序中获得一个Runtime实例,只能:
Runtime run = Runtime.getRuntime();
|
案例:
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
System.out.println("处理器的个数:"+run.availableProcessors()+"个");
System.out.println("空闲内存数量:"+run.freeMemory()/1024/1024+"M");
System.out.println("最大可用内存数量:"+run.maxMemory()/1024/1024+"M");
}
|
Exec()方法
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
//打开记事本程序
run.exec("notepad.exe");
}
|
Djdestroy()方法
public static void main(String[] args) throws Exception {
Runtime run = Runtime.getRuntime(); //创建一个Runtime实例对象
Process exec = run.exec("notepad.exe"); //得到表示进程的Process对象
Thread.sleep(3000); //程序休眠3秒
exec.destroy(); //杀掉进程
}
|
|
Math类与Random类
包装类:(Java自动实现装箱和拆箱)
Date类、Calendar类与DateFormat类
JDK7新特性——switch语句支持字符串类型