您的位置:首页 > 编程语言 > Java开发

java基础十二天 常用api

2017-08-16 09:11 330 查看

1、String

常用方法:

public int length()  //获取字符串长度
public char charAt(int index) //根据索引查找索引位置的字符
public boolean equals(Object anObject) //比较两个字符串的内容是否相等 它是从写了object的方法
public boolean equalsIgnoreCase(String anotherString) //不区分大小写的内容比较
public int compareTo(String anotherString) //比较两个字符串的大小,用于排序
public int indexOf(String s) //查看某个字符串在当前字符串第一次出现的位置,找不到返回-1
public int indexOf(String s ,int startpoint)
public int lastIndexOf(String s) //找出某个字符串中在当前字符串中最后一次出现的位置
public int lastIndexOf(String s ,int startpoint)
public boolean startsWith(String prefix) //判断某个字符串是否以prefix开头
public boolean endsWith(String suffix) //判断某个字符串是否以suffix结尾
public boolean isEmpty() //判断字符串是否为空串, 当且仅当 length() 为 0 时返回 true


字符串对象修改

public String substring(int startpoint)  //字符串截取,从startpoint开始,到最后
public String substring(int start,int end) //字符串截取,从start到end结束,但不包含end
pubic String replace(char oldChar,char newChar) //替换字符串,把oldChar替换为newChar
public String replaceAll(String regex, String replacement) //替换字符串,根据正则表达式替换
public String trim() //去掉字符串两端的空格,但是中间的去不掉
public String concat(String str) //拼接字符串
public String[] split(String regex) //将字符串按照规则regex进行切分,返回数组
public char[] toCharArray() 将此字符串转换为一个新的字符数组。
public String toLowerCase() 将此 String 中的所有字符都转换为小写
public String toUpperCase() 将此 String 中的所有字符都转换为大写
public static String valueOf(...) 将基本类型转为字符串


2、StringBuffer

StringBuffer:是一个字符串容器

特点:

长度可变,能够追加保存字符串

线程安全

访问慢

构造方法:

1.StringBuffer()初始容量为16的字符串缓冲区
2.StringBuffer(int size)构造指定容量的字符串缓冲区
3.StringBuffer(String str)将内容初始化为指定字符串内容

public int capacity()//返回当前容量
public int length() //返回字符串长度


常用方法:

StringBuffer append(String s) //追加字符串到原字符串的尾部,最常用

StringBuffer insert(int index, String str) //插入字符串,较少使用
public StringBuffer reverse() //反转,较少使用
StringBuffer delete(int startIndex, int endIndex) //根据开始和结束位置删除
public char charAt(int n ) //返回某个位置的字符
public void setCharAt(int n ,char ch) //在某个位置上设置字符
StringBuffer replace( int startIndex ,int endIndex, String str) //根据范围替换
public int indexOf(String str) //查找字符在StringBuffer第一次出现的位置
public String substring(int start,int end) //截取字符串


StringBuilder:

StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且方法也一样

最主要区别:StringBuilder是非线程安全,操作快

面试题:String,StringBuffer,StringBuilder的区别

String:不可变字符序列

StringBuffer:可变字符序列、效率低、线程安全

StringBuilder(JDK1.5):可变字符序列、效率高、线程不安全

3、System

常用方法:

static long currentTimeMillis()
返回以毫秒为单位的当前时间。
static void exit(int status)
终止当前正在运行的 Java 虚拟机。
static void gc()
运行垃圾回收器。
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。


4、Date

日期Date:表示特定的瞬间,精确到毫秒

构造方法:

Date()使用Date类的无参数构造方法创建的对象可以获取本地当前时间。
Date(long date) 根据传入的时间戳来创建时间


常用方法:

getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。


SimpleDateFormat类用于日期格式化:

构造方法:

SimpleDateFormat()
用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。


常用功能:

1、格式化(日期-》文本) public final String format(Date date)

2、转换 (文本-》日期) public Date parse(String source)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java api string