(String中关于位置的是起始位置和长度,StringBuffer关于位置的是起始位置和终止位置,StringBuffer更像数组)
一、String
字符串最大特点:一旦初始化就不能再被修改(不可变对象,可以浅拷贝)
String s1=”abc”;//s1类似于指针,指向对象“abc”。
Object作为所有类类型的超类,其方法equals比较的是两个对象的内存地址是否相同。
而String类覆盖了这个方法,比较的是字符串的内容
String s1=”a”;
String s2=new String(“a”);
String s3=”a”;
一共有2个a,s1指向一个常量”a”,s3初始化时发现已存在的s1指向的常量”a”,则不会再另开辟空间分配。s2是通过new创建的a,位于堆内存中。
(s1==s2)==false
(s1==s3)==true
String类方法
1.获取
字符长度:length()//数组的length是字段,不是方法
字符串上某个位置的字符:char charAt(int index)
根据字符返回其在字符串第一次出现的位置:
int indexOf(int ch)//没有则返回-1,传入的不是char类型而是int类型
int indexOf(int ch,int fromIndex)//从指定位置开始查询,但返回的是从字符首位开始的位置。
反向索引字符出现位置,返回的是字符最后一次出现的位置:
int lastIndexOf(int ch)
2.判断(返回boolean)
是否包含某一子串contains(CharSequence str)
字符串是否为空 isEmpty()
字符串是否以指定内容开头/结尾startWith(String prefix)/endWith(String suffix)
字符串内容是否相同 equals()
判断字符串内容是否相同,但是忽略大小写equalsIgnoreCase()
3.转换
字符数组转换为字符串
a通过构造函数
new String(buf)
new String(buf,0,cntBuf)
b通过静态方法
static String copyValueOf(char[]data)
返回指定数组中表示该字符序列的 String。
static String copyValueOf(char[]data, int offset, int count)
返回指定数组中表示该字符序列的 String。
valueOf()可以返回多种类型的数据的字符串表示形式,其中也有字符数组:
static String valueOf(char[]data)
返回 char 数组参数的字符串表示形式。
字符串转换为字符数组:
char[] toCharArray();
字节数组转换为字符串:通过构造函数
String String(byte[]buf,int offset,intcount)
字符串转为字节数组
byte[] getBytes()
基本数据类型转换为字符串
valueOf(…)
字符串转为基本数据类型
使用基本数据类型的包装类的parseTypeName(String data)
字符串和字符数组在转换过程中可以指定编码表,想做编码操作必须使用字符数组,字符无法进行编码操作
4.替换
字符替换
String replace(char oldChar,char newChar)
String s=”hello”;Strings2=s.replace(‘o’,‘b’);//s2=”hellb”;
子串替换
String replace(CharSequencetarget,CharSequence replacement)
5.切割
String[] split(String regex, int limit)
6.子串
String substring(int beginIndex,intendIndex)//包括头部包括尾
如果endIndex超出范围,会产生下标越界异常
获取整个字符串,s.substring(0,s.length())
7.其他
大小写 toUpperCase()/toLowerCase()
去除头尾多余空格trim()
自然顺序比较两个字符串
int compareTo(String s)
二、StringBuffer和StringBuider
api升级的三个因素:1.提高效率2.简化书写3.安全性
StringBuffer(线程安全) – 升级版StringBuider(效率高,但不保证同步)
是字符串的一个容器
三个特点:
1.长度可变
2.可操作多种数据类型
3.通过toString()方法变成字符串
方法:
1.存储
append(…)//将多种类型的数据添加到末尾,返回this,本类StringBuffer对象
可以连续调用(方法调用链)s.append(3).append(“asda”);
insert(int offset,…)//在offset处插入…多种类型的数据,offset不要下标越界
2.删除(返回this)
delete(int start,int end)//不包含end
deleteCharAt(int index)//删除index处的字符
sb.delete(0,sb.length());//清空缓冲区
sb.delete(1,1);//什么都不删除
sb.delete(2,1);//报错
3.获取
int length()
char charAt(int index)//获取index处的字符
int indexOf(String str)
int lastIndexof(String str)
String substring(int start,int end)
void getChars(int srcBegin,intsrcEnd,char[]dst,int dstBegin)// 将字符从此序列复制到目标字符数组 dst。超出下标会出异常:
IndexOutOfBoundsException- 如果以下任意一项为 true:
srcBegin 为负
dstBegin 为负
srcBegin 参数大于 srcEnd 参数。
srcEnd 大于 this.length()。
dstBegin+srcEnd-srcBegin大于 dst.length
4.修改
void setCharAt(int index,char ch)
StringBuffer replace(int start,intend,String str)//用str替换start至end-1的字符
5.反转
StringBuffer reverse()
三、基本数据类型的包装类
基本数据类型:
void byte int char long short double floatboolean
除了void都有对应的引用型数据类型类:
Byte Integer Character Long Short DoubleFloat Boolean
以Integer为例:
1.最常见操作:用于基本数据类型与字符串之间的转换
String data=”123”;
int a=Ingeter.parseInt(data);
整型转字符串:
String toString()
返回一个表示该 Integer 值的 String 对象。
static String toString(int i)
返回一个表示指定整数的 String 对象。
static String toString(int i, int radix)
返回用第二个参数指定基数表示的第一个参数的字符串表示形式。
后两个是静态方法不需要创建Integer对象
2.位移:
static int rotateLeft/rotateRight(int I,intdistance)
3.进制转换:
十进制转为其他:
static String toHexString()
static String toBinaryString()
static String toOctalString()
其他转为十进制:
parseInt(…,intradix)//将…按照radix的进制表示为整型返回
int a=Integer.parseInt(“BA”,16);
构造函数传入有效的字符串
int b=Integer(“123”);
intValue()//非静态方法将对象的int值返回
Integer覆盖了equals比较的是数据是否相同 ==则比较的是地址
jdk1.5新特性,自动装箱和自动拆箱
原:Integer x=newInteger(“4”);
现:Integer x=4;//4是对象,自动实现了new Integer(“4”),x直接指向这个对象。
x=x+2;//1.x自动拆箱为4,4+2=6,6自动装箱new Integer(6),x再指向此对象
因为Integer是类,所以x=null成立
在新特性下,如果x的数值在byte范围内,若此值已存在,则不会另外开辟新的空间,而是直接指向这个值。大于byte的范围则会再开辟空间存储
Integer a1=127;
Integer b1=127;
//a1==b1
Integer a2=128;
Integer b2=128;
//a2!=b2
=========
150527 add:
Character包装类有方法 static boolean isAlphabetic(char c)可以判断字符是否是字母。