【java】开发中常用字符串方法

java字符串的功能可以说非常强大, 它的每一种方法也都很有用.

java字符串中常用的有两种字符串类, 分别是String类和StringBuffer类.

Sting类

String类的对象是不可变的.

创建String

String()
String(String str)
String(char value[]) //用字符数组生成一个串对象
String(char value[], int offset, int count) //用字符数组value的offset位开始的count个字符,建立一个字符串对象

 

常用方法

int length()
String toLowerCase()//返回当前串的小写
String toUpperCase()//返回当前串的大写
char[] toCharArray()//返回当前串的字符串数组
String trim()//删除当前字符串的前部和后部空格并返回新串

实例:

String str = new String("    Hello world    ");
str.length();    //返回str的长度为11
str.toLowerCase();//将str转换为小写, "    hello world    "
str.toUpperCase();//将str转换为大写, "    HELLO WORLD    "
char[] strChar = str.toCharArray();//将str转换为strChar字符数组
str.trim();//删除前后的空格, "Hello world"

 

比较性质的方法

boolean regionMatches(int toffset, String str, int ooffset, int len)//比较从本串的toffset开始的len个字符和str从ooffset开始的len个字符是否一致, 一致则返回true(可用来检测字符换字符串在当前串中出现的次数)
boolean regionMatches(boolean IgnoreCase, int toffset, String str, int ooffset, int len)//同上, IgnoreCase决定是否忽略大小写, IgnoreCase为true时忽略大小写
String concat(String str)//返回当前字符串与str串连接后的新串
int compareTo(String str)//比较字符串中相同位置的Unicode, 若两串相等返回0, 否则当前串大于str返回比较字符的差值
int compareToIgnoreCase(String str)//忽略大小写比较, 同上
boolean equals(Object anObj)//比较两个对象的值是否相等.这里比较两个字符串对象是否相等
boolean equalsIgnoreCase(String anotherString)//忽略大小写, 比较两个字符串对象是否相等
boolean startsWith(String prefix[, int toffset])//判断当前字符串从toffset开支是否以参数prefix开头, []中括号表示可省略
boolean endsWidth(string prefix[, int toffset])//判断当前字符串从toffset开始是否以参数prefix结尾

 

查找方法

//字符ch查找, 注意是字符
int indexOf(int ch)//从前向后找第一个字符ch出现的位置, 未找到返回-1
int indexOf(int ch, int fromIndex)//从fromIndex位置开始向后找第一个字符ch出现的位置, 未找到返回-1
int lastIndexOf(int ch)//从后向前找第一个字符ch出现的位置, 未找到返回-1
int lastIndexOf(int ch, int fromIndex)//从fromIndex位置开始前后找第一个字符ch出现的位置, 未找到返回-1
//子串str查找
int indexOf(String str)//从当前字符串开头向后查找子串str第一次出现的位置, 未找到返回-1
int indexOf(String str, int fromIndex)//从当前字符串的fromIndex位置向后查找子串str第一次出现的位置, 未找到返回-1
int lastIndexOf(String str)//从当前字符串结尾向前查找子串str第一次出现的位置, 未找到返回-1
int lastIndexOf(String str, int fromIndex)//从当前字符串的fromIndex位置向前查找子串str第一次出现的位置, 未找到返回-1
char charAt(int index)//返回当前字符串index位置处的字符

 

替换方法

//替换
String replace(char oldchar, char newchar)//将字符串中所有oldcha字符r替换为newchar字符
String replaceFirst(String regex, String replacement)//将字符串中第一个与正则表达式regex匹配的子串用新串replacement替换
String replaceAll(String regex, String replacement)//将字符串中所有与正则表达式regex匹配的子串用新串replacement替换
String substring(int start[, int end])//返回start到end-1返回的子串, 若省略end, 则为start到串尾.
String[] split(String regex)//返回当前字符串通过正则表达式分割的字符串数组

 

其他方法

将数字化的字符串转换为基本类型

public static  byte  parseByte(String  s) throws NumberFormatException
public static  short  parseShort(String  s) throws NumberFormatException
public static  short  parseInt(String  s) throws NumberFormatException
public static  long  parseLong(String  s) throws NumberFormatException
public static  float  parseFloat(String  s) throws NumberFormatException
public static  double  parseDouble(String  s) throws NumberFormatException

用法举例:

int a = Integer.parseInt(“23”);

 

其他类型转换为字符串

public static String valueOf(int n)
public static String valueOf(char[] data)
public static String valueOf(Object obj)
public static String copyValueOf(char[] data)等同于valueOf(char[] data)

用法举例:

String.valueOf(334);

 

StringBuffer类

StringBuffer()//创建空StringBuffer对象
StringBuffer(int length)//创建一个长度为length的StringBuffer对象
StringBuffer(String str)//创建一个str字符串StringBuffer对象

StringBuffer append(Object obj)//将对象obj添加到StringBuffer对象中
StringBuffer insert(int position, Object obj)//将对象obj插入到StringBuffer对象中的position位置
StringBuffer setCharAt(int position, char ch)//用字符ch替换StringBuffer对象中的position位置
StringBuffer deleteCharAt(int position)//删除position位置的字符
StringBuffer replace(int start, int end, String str)//将StringBuffer对象中start到end-1的位置用字符串str替换

 

posted @ 2017-06-15 07:58  天秤libra  阅读(614)  评论(0编辑  收藏  举报