String

来源:互联网 发布:ubuntu golang 1.8 编辑:程序博客网 时间:2024/06/14 00:44

String

  • String一旦被赋值,便不能更改其指向的字符对象,如果更改,则会指向一个新的字符对象。
  • 当String的变量作为参数传递到函数中,即使函数中改变了它的值,但对函数外是没有影响。(这个是String的特例,其他引用类型一般都会产生影响的)

StringBuffer

StringBuffer的构造函数

  • StringBuffer()
  • StringBuffer(int size)
  • StringBuffer(String str)
  • StringBuffer(CharSequence chars)

length()和capacity()

length()计算StringBuffer当前长度,而整个可分配空间通过capacity()方法得到。

ensureCapacity() 设置缓冲区的大小

void ensureCapacity(int capacity)

setLength() 设置缓冲区的长度

void setLength(int len)

charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)

getChars()

void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

append() 可把任何类型数据的字符串表示连接到调用它的StringBuffer对象的末尾。

例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(”a=”).append(a).append(”!”).toString();

insert() 插入字符串

StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定将字符串插入到StringBuffer对象中的位置的下标。

reverse() 颠倒StringBuffer对象中的字符

StringBuffer reverse()

delete()和deleteCharAt() 删除字符

StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)

replace() 替换

StringBuffer replace(int startIndex,int endIndex,String str)

substring() 截取子串

String substring(int startIndex)
String substring(int startIndex,int endIndex)

字符串转数字的函数

原文出处

转换字符串为数字

/** 转换字符串为数字 */public static int strToInt(String value, int defaultValue) {     try {         return Integer.valueOf(value);     } catch (Exception e) {         return defaultValue;     } } 

转换字符串为数字

/** 转换字符串为数字 */public static long strToInt(String value, long defaultValue) {     try {         return Long.valueOf(value);     } catch (Exception e) {         return defaultValue;     } } 

转换16进制字符串为数字

/** 转换16进制字符串为数字 */public static int hexToInt(String value, int defaultValue) {     try {         return Integer.parseInt(value, 16);     } catch (Exception e) {         return defaultValue;     } } 

转换16进制字符串为数字

/** 转换16进制字符串为数字 */public static long hexToInt(String value, long defaultValue) {     try {         return Long.parseLong(value, 16);     } catch (Exception e) {         return defaultValue;     } } 

转换字符串为数字

/** 转换字符串为数字 */public static float strToFloat(String value, float defaultValue) {     try {         return Float.valueOf(value);     } catch (Exception e) {         return defaultValue;     } } 

转换字符串为数字

/** 转换字符串为数字 */public static double strToDouble(String value, double defaultValue) {     try {         return Double.valueOf(value);     } catch (Exception e) {         return defaultValue;     } }
0 0
原创粉丝点击