文档库 最新最全的文档下载
当前位置:文档库 › API常用类(String类)

API常用类(String类)

API常用类(String类)
API常用类(String类)

●boolean equals(Object obj):比较字符串的内容是否相同,区分大小写

●boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,

忽略大小写

●boolean contains(String str):判断大字符串中是否包含小字符串

●boolean startsWith(String str):判断字符串是否以某个指定的字符串

开头

●boolean endsWith(String str):判断字符串是否以某个指定的字符串

结尾

●boolean isEmpty():判断字符串是否为空

String类的获取功能

●int length():获取字符串的长度

●char charAt(int index):获取指定索引位置的字符

●int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引

●int indexOf(String str):返回指定字符串在此字符串中第一次出现处的

索引

●int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定

位置后第一次出现处的索引

●int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从

指定位置后第一次出现处的索引

●String substring(int start):从指定位置开始截取字符串,默认到末尾

●String substring(int start,int end):从指定位置开始到指定位置结束截

取字符串

●byte[] getBytes():把字符串转换为字节数组

●char[] toCharArray():把字符串转换为字符数组

●static String valueOf(char[] chs):把字符数组转成字符串

●static String valueOf(int i):把int类型的数据转成字符串

●String toLowerCase():把字符串转成小写

●String toUpperCase():把字符串转成大写

●String concat(String str):把字符串拼接

String类的其他功能:

●替换功能

?String replace(char old,char new)

?String replace(String old,String new)

●去除字符串两空格

?String trim()

●按字典顺序比较两个字符串

?int compareTo(String str)

?int compareToIgnoreCase(String str)

StringBuffer类的成员方法:

●添加功能

?public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身

?public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里,并返回字符串缓冲区本身

●删除功能

?public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身

?public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身

●替换功能

?public StringBuffer replace(int start,int end,String str):从start开始到end用str替换

●反转功能

public StringBuffer reverse():字符串反转

●截取功能

?public String substring(int start)

?public String substring(int start,int end)

●返回值类型不再是StringBuffer本身了

●截取功能和前面几个功能的不同

?返回值类型是String类型,本身没有发生改变

Arrays类概述及其常用方法

●Arrays类概述

?针对数组进行操作的工具类。

?提供了排序,查找等功能。

●成员方法

?public static String toString(int[] a):数组转成字符串

?public static void sort(int[] a):对数组进行排序

?public static int binarySearch(int[] a,int key):折半查找

Integer类成员方法

●int类型和String类型的相互转换

●int – String:String.valueOf(number)

●String – int:Integer.parseInt(s)

●public int intValue()

●public static int parseInt(String s)

●public static String toString(int i)

●public static Integer valueOf(int i)

●public static Integer valueOf(String s)

●常用的基本进制转换

?public static String toBinaryString(int i)

?public static String toOctalString(int i)

?public static String toHexString(int i)

●十进制到其他进制

?public static String toString(int i,int radix)

●其他进制到十进制

?public static int parseInt(String s,int radix)

●JDK1.5以后,简化了定义方式。

?Integer x = new Integer(4);可以直接写成

?Integer x = 4;//自动装箱。

?x = x + 5;//自动拆箱。通过intValue方法。

Character类概述及其构造方法

●Character类概述

?Character 类在对象中包装一个基本类型 char 的值

?此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然

●构造方法

?public Character(char value)

●public static boolean isUpperCase(char ch):判断给定的字符是否是

大写字符

●public static boolean isLowerCase(char ch):判断给定的字符是否是

小写字符

●public static boolean isDigit(char ch):判断给定的字符是否是数字字

●public static char toUpperCase(char ch):把给定的字符转换为大写字

●public static char toLowerCase(char ch):把给定的字符转换为小写字

正则表达式的应用:

常见规则

A:字符

x 字符x。举例:'a'表示字符a

\\ 反斜线字符。

\n 新行(换行)符('\u000A')

\r 回车符('\u000D')

B:字符类

[abc] a、b 或c(简单类)

[^abc] 任何字符,除了a、b 或c(否定)

[a-zA-Z] a到z 或A到Z,两头的字母包括在内(范围)

[0-9] 0到9的字符都包括

C:预定义字符类

. 任何字符。我的就是.字符本身,怎么表示呢? \.

\d 数字:[0-9]

\w 单词字符:[a-zA-Z_0-9]

在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器

^ 行的开头

$ 行的结尾

\b 单词边界

就是不是单词字符的地方。

举例:hello world?haha;xixi

E:Greedy 数量词

X? X,一次或一次也没有

X* X,零次或多次

X+ X,一次或多次

X{n} X,恰好n 次

X{n,} X,至少n 次

X{n,m} X,至少n 次,但是不超过m 次

●判断功能

?public boolean matches(String regex)

●分割功能

?public String[] split(String regex)

●替换功能

?public String replaceAll(String regex,String replacement) ●获取功能

?Pattern和Matcher类的使用

Math类概述及其成员方法:

●Math类概述

?Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

成员变量:

?public static final double E:自然对数的底数

?public static final double PI:π

●成员方法

?public static int abs(int a):绝对值

?public static double ceil(double a):向上取整

?public static double floor(double a):向下取整

?public static int max(int a,int b):最大值

?public static double pow(double a,double b):a的b次幂

?public static double random():随机数

?public static int round(float a): 四舍五入

?public static double sqrt(double a):正平方根

●Random类概述

?此类用于产生随机数

?如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

●构造方法

?public Random():没有给种子,用的是默认种子,是当前时间

的毫秒值

?public Random(long seed):给出指定的种子,给定种子后,每

次得到的随机数是相同的。

Random类成员方法:

●public int nextInt():返回的是int范围内的随机数

●public int nextInt(int n):返回的是[0,n)范围的内随机数

●System类概述:

●System 类包含一些有用的类字段和方法。它不能被实例化。

●成员方法

●public static void gc():运行垃圾回收器。

●public static void exit(int status):终止当前正在运行的 Java

虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终

止。

●public static long currentTimeMillis():返回以毫秒为单位的当

前时间

●public static void arraycopy(Object src,int srcPos,Object

dest,int destPos,int length):从指定源数组中复制一个数组,

复制从指定的位置开始,到目标数组的指定位置结束。

BigInteger类概述:

?可以让超过Integer范围内的数据进行运算

●构造方法

?public BigInteger(String val)

BigInteger类成员方法

●public BigInteger add(BigInteger val):加

●public BigInteger subtract(BigInteger val):减

●public BigInteger multiply(BigInteger val):乘

●public BigInteger divide(BigInteger val):除

●public BigInteger[] divideAndRemainder(BigInteger val):返回商和

余数的数组

BigDecimal类概述及其构造方法

●由于在运算的时候,float类型和double很容易丢失精度,演

示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

●BigDecimal类概述

?不可变的、任意精度的有符号十进制数。

●构造方法

?public BigDecimal(String val)

BigDecimal类成员方法

●public BigDecimal add(BigDecimal augend):加

●public BigDecimal subtract(BigDecimal subtrahend):减

●public BigDecimal multiply(BigDecimal multiplicand):乘

●public BigDecimal divide(BigDecimal divisor):除

●public BigDecimal divide(BigDecimal divisor,int scale,

int roundingMode):商,几位小数,如何舍取

Date类概述及其方法

●Date类概述

?类 Date 表示特定的瞬间,精确到毫秒。

●构造方法

?public Date()

?public Date(long date)

●成员方法

?public long getTime():获取毫秒值

?public void setTime(long time)

DateFormat类概述及其方法

●DateFormat类概述

?DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。

?是抽象类,所以使用其子类SimpleDateFormat

●SimpleDateFormat构造方法

?public SimpleDateFormat():默认模式

?public SimpleDateFormat(String pattern):给定的模式

●成员方法

?public final String format(Date date)

?public Date parse(String source)

Calendar类概述及其方法

●Calendar类概述

?Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并

为操作日历字段(例如获得下星期的日期)提供了一些方法。

●成员方法

?public static Calendar getInstance():获取当前日历时间

?public int get(int field):获取年或月或日

?public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作

?public final void set(int year,int month,int date):设置当前日历的年月日

相关文档