您的位置:首页 > 产品设计 > UI/UE

JAVA基础学习day13--String、StringBuilder与StringBuffer与包装类

2015-09-21 10:15 781 查看

一、String

1.1、String



String 类是final修饰的,是顶级类,不可被继承

String
类代表字符串。Java 程序中的所有字符串字面值(如
"abc"
)都作为此类的实例实现。

字符串是常量;它们的值在创建之后不能更改

String str="abc";

String
类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写。

1.2、String常用方法

方法摘要
char
charAt(int index)

返回指定索引处的
char
值。
int
codePointAt(int index)


返回指定索引处的字符(Unicode 代码点)。
int
codePointBefore(int index)


返回指定索引之前的字符(Unicode 代码点)。
int
codePointCount(int beginIndex,
int endIndex)

返回此
String
的指定文本范围中的 Unicode
代码点数。
int
compareTo(String anotherString)


按字典顺序比较两个字符串。
int
compareToIgnoreCase(String str)


按字典顺序比较两个字符串,不考虑大小写。
String
concat(String str)


将指定字符串连接到此字符串的结尾。
boolean
contains(CharSequence s)


当且仅当此字符串包含指定的char 值序列时,返回 true。
boolean
contentEquals(CharSequence cs)


将此字符串与指定的
CharSequence
比较。
boolean
contentEquals(StringBuffer sb)


将此字符串与指定的
StringBuffer
比较。
staticString
copyValueOf(char[] data)


返回指定数组中表示该字符序列的 String。
staticString
copyValueOf(char[] data,
int offset,int count)

返回指定数组中表示该字符序列的 String。
boolean
endsWith(String suffix)


测试此字符串是否以指定的后缀结束。
boolean
equals(Object anObject)


将此字符串与指定的对象比较。
boolean
equalsIgnoreCase(String anotherString)


将此
String
与另一个
String

比较,不考虑大小写。
staticString
format(Locale l,String format, Object... args)


使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
staticString
format(String format, Object... args)


使用指定的格式字符串和参数返回一个格式化字符串。
byte[]
getBytes()


使用平台的默认字符集将此
String
编码为 byte 序列,并将结果存储到一个新的 byte
数组中。
byte[]
getBytes(Charsetcharset)


使用给定的 charset 将此
String

编码到 byte 序列,并将结果存储到新的 byte 数组。
void
getBytes(int srcBegin,
int srcEnd,byte[] dst,int dstBegin)


已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过
getBytes()

方法,该方法使用平台的默认字符集。
byte[]
getBytes(StringcharsetName)


使用指定的字符集将此
String
编码为 byte 序列,并将结果存储到一个新的 byte
数组中。
void
getChars(int srcBegin,
int srcEnd,char[] dst,int dstBegin)


将字符从此字符串复制到目标字符数组。
int
hashCode()


返回此字符串的哈希码。
int
indexOf(int ch)


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

返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
int
indexOf(String str)


返回指定子字符串在此字符串中第一次出现处的索引。
int
indexOf(String str,
int fromIndex)

返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
String
intern()


返回字符串对象的规范化表示形式。
boolean
isEmpty()


当且仅当
length()

0 时返回 true。
int
lastIndexOf(int ch)


返回指定字符在此字符串中最后一次出现处的索引。
int
lastIndexOf(int ch,
int fromIndex)


返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
int
lastIndexOf(String str)


返回指定子字符串在此字符串中最右边出现处的索引。
int
lastIndexOf(String str,
int fromIndex)


返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
int
length()


返回此字符串的长度。
boolean
matches(String regex)


告知此字符串是否匹配给定的正则表达式
int
offsetByCodePoints(int index,
int codePointOffset)

返回此
String
中从给定的
index
处偏移
codePointOffset
个代码点的索引。
boolean
regionMatches(boolean ignoreCase,
int toffset,String other,int ooffset,
int len)

测试两个字符串区域是否相等。
boolean
regionMatches(int toffset,
String other,
int ooffset,int len)

测试两个字符串区域是否相等。
String
replace(char oldChar,
char newChar)

返回一个新的字符串,它是通过用
newChar

替换此字符串中出现的所有
oldChar
得到的。
String
replace(CharSequence target,CharSequence replacement)


使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String
replaceAll(String regex,String replacement)


使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
String
replaceFirst(String regex,String replacement)


使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
String[]
split(String regex)

根据给定正则表达式的匹配拆分此字符串。
String[]
split(String regex,
int limit)

根据匹配给定的正则表达式来拆分此字符串。
boolean
startsWith(String prefix)


测试此字符串是否以指定的前缀开始。
boolean
startsWith(String prefix,
int toffset)

测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
CharSequence
subSequence(int beginIndex,
int endIndex)

返回一个新的字符序列,它是此序列的一个子序列。
String
substring(int beginIndex)


返回一个新的字符串,它是此字符串的一个子字符串。
String
substring(int beginIndex,
int endIndex)

返回一个新字符串,它是此字符串的一个子字符串。
char[]
toCharArray()


将此字符串转换为一个新的字符数组。
String
toLowerCase()


使用默认语言环境的规则将此
String
中的所有字符都转换为小写。
String
toLowerCase(Locale locale)

使用给定
Locale
的规则将此
String
中的所有字符都转换为小写。
String
toString()


返回此对象本身(它已经是一个字符串!)。
String
toUpperCase()


使用默认语言环境的规则将此
String
中的所有字符都转换为大写。
String
toUpperCase(Locale locale)

使用给定
Locale
的规则将此
String
中的所有字符都转换为大写。
String
trim()


返回字符串的副本,忽略前导空白和尾部空白。
staticString
valueOf(boolean b)


返回
boolean
参数的字符串表示形式。
staticString
valueOf(char c)


返回
char
参数的字符串表示形式。
staticString
valueOf(char[] data)


返回
char
数组参数的字符串表示形式。
staticString
valueOf(char[] data,
int offset,int count)

返回
char

数组参数的特定子数组的字符串表示形式。
staticString
valueOf(double d)


返回
double
参数的字符串表示形式。
staticString
valueOf(float f)


返回
float
参数的字符串表示形式。
staticString
valueOf(int i)


返回
int
参数的字符串表示形式。
staticString
valueOf(long l)


返回
long
参数的字符串表示形式。
staticString
valueOf(Object obj)


返回
Object
参数的字符串表示形式。
1.3、String str="abc"与String str1=new String("abc")区别

package com.pb.string.demo1;

public class Demo1 {

public staticvoid main(String[] args) {
/*
* s1一个字符串类类型变量,abc是一个对象
* 一旦被初始化,就不可以被改变
*/
String str1="abc";
/*
* new 就是在堆内存中再开辟一个新的空间
* s1只有一个对象abc
* s2有2个对象abc和new String("abc");
*/
String str2=new String("abc");

System.out.println(str1==str2);//false ==比较内存地址
/*
* String 类重写了Object中的equals方法,用来比较字符串内容是否相同
*/
System.out.println(str1.equals(str2));//true 比较地址中的值

}

}


1.4、获取和判断

获取

获取字符串长度length();

根据位置获取位置下的字符charcharAt(int index);

根据字符获取该字字符串中位置int indexOf(in ch)返回第一次出现的位置

从fromIndex(int ch,int fromIndex):获取ch在字符串中出现位置

从fromIndex(String str,int fromIndex):获取str在字符串中出现位置

package com.pb.string.demo1;

public class StringDemo1 {

public staticvoid main(String[] args) {
String str="abcdefghijklmnab"; //长度16
//长度
sop(str.length());
//根据索引获取字符
sop(str.charAt(5));
//sop(str.charAt(33));//下标越界
//根据字符获取索引
sop(str.indexOf('b')); //从0开始查找
//从指字位置开始
sop(str.indexOf('b',5));//从索引为5的字符形开始查找
//查找没的字符
sop(str.indexOf('v',5));//-1 如果没有找到返回-1
//从后向前查找
sop(str.lastIndexOf('b'));
sop(str.lastIndexOf("b"));
//
}
public staticvoid sop(Object obj){
System.out.println(obj);
}

}


判断

字符串中是否包含某一个子串boolean contains(String str); indexOf(str) 可以索引str第一次出现的位置,如果返回-1,表示不包含在字符串,也可以用于指定判断是否包含

字符串中是否有内容boolean isEmpty(),判断长度是否为0,

字符串是否以指字内容开头boolean startWith(String str)

字符串是否以指字内容结尾boolean endWith(String str)

判断字符内容是否相同equals(String str),重写Object中equals方法

判断字符内容是否相同忽略大小写equalsIgnoreCase(String str),

package com.pb.string.demo1;

public class StringDemo2 {

public staticvoid main(String[] args) {
String str="ArrayDemo.java";
sop(str.startsWith("Array")); //判断是否以Array开头 true
sop(str.endsWith(".java"));//判断是否以.java结尾 true
sop(str.contains("Demo"));//判断是否有(包含)Demo子串 true
sop(str.equals("ArrayDemo.java"));//判断字符串内容是否相同 true
sop(str.equalsIgnoreCase("ArrayDemo.java"));//判断字符串内容是否相同并忽略大小字书 true
}

public staticvoid sop(Object obj){
System.out.println(obj);
}
}


[b]1.5、转换[/b]

将字符数组转成字符串--构造方法

package com.pb.string.demo1;

public class StringDemo3 {

public staticvoid main(String[] args) {
char [] ch={'a','b','c','d','e','f','g'};
//将字符数组转换成字符串
String str1=new String(ch);
sop(str1);
String str2=new String(ch,1,4);//第下标为1开始转换4个字符s
sop(str2);
//静态方法
String str3=String.copyValueOf(ch);
sop(str3);
String str4=String.copyValueOf(ch,1,5);
sop(str4);
String str5=String.valueOf(ch);
sop(str5);
String str6=String.valueOf(ch,1,4);
sop(str6);

}
public staticvoid sop(Object obj){
System.out.println(obj);
}
}


将字符串,转换成字符数组

将字节数组转换成字符串

char[]
toCharArray()

将此字符串转换为一个新的字符数组
将字符串转换成字节数组

package com.pb.string.demo1;

public class StringDemo4 {

public staticvoid main(String[] args) {
String str="abcdefg";
//将字符串转换为字符数组
char [] ch=str.toCharArray();
//遍历
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]+",");
}
//将字符串转换为字节数组
byte [] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.print((char)b[i]);
}

}

}


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

package com.pb.string.demo1;

public class StringDemo5 {

public staticvoid main(String[] args) {
int i=3; //可以是double,float,short,long,char,byte
String str1=i+"";
String str2=String.valueOf(i);

}

}


1.6、切割和替换,截取

分离



package com.pb.string.demo1;

import java.util.Arrays;

public class SplitString {

public staticvoid main(String[] args) {
String str1="zhangsa,lisi,wangwu,zhaoliu";
String[] arr=str1.split(",");
System.out.println(Arrays.toString(arr));

}

}


结果:[zhangsa, lisi, wangwu, zhaoliu]

替换



package com.pb.string.demo1;

public class ReplaceDemo {

public staticvoid main(String[] args) {
String str1="Hello,Java!";
String str2=str1.replace('a', 'n');
String str3=str1.replace('q', 'n');//如果替换的不在,返回的还是原来的
String str4=str1.replace("Java", "World");
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);

}

}


Hello,Java!
Hello,Jnvn!
Hello,Java!
Hello,World!

截取





package com.pb.string.demo1;

public class SubStringDemo6 {

public staticvoid main(String[] args) {
String s="abcdef";
System.out.println(s.substring(2));//cdef,从第二开始到结尾
System.out.println(s.substring(2, 4));//cd 从第二个到第4个但不包含第4个
System.out.println(s.substring(0,s.length())); //获取全部
System.out.println(s.substring(0));
}

}


[b]1.7、比较和去除空格[/b]

比较



如果参数字符串等于此字符串,则返回值
0
;如果此字符串按字典顺序小于字符串参数,则返回一个小于
0
的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于
0
的值。

去空格

将字符串转换为大写或者小写



package com.pb.string.demo1;

public class StringDemo6 {

public staticvoid main(String[] args) {
String s=" Hello Java! ";
//转换为小写
System.out.println(s.toLowerCase());
//转换为大写
System.out.println(s.toUpperCase());
//去除2边的空格
System.out.println(s.trim());
}

}


结果:

hello java!
HELLO JAVA!
Hello Java!


1.8、去掉字符串两边的空格

package com.pb.string.demo2;
/**
* 模拟一个trim方法,去除字符串2边的空格
* @author Denny
* 判断字符串开始第一个位置是不是空格,是断续直到不是空格 为步
* 结尾处判断空格一样
* 当开始和结尾都不是空格时,就是要获取的字符串
*
* */
public class StringDemo1 {

public staticvoid main(String[] args) {
String str=" Hello Java! ";
sop(str);//去空格前
str=myTrim(str);
sop(str);//去空格后
}
public static String myTrim(String str){
int start=0,end=str.length()-1;
//开始的空格
while(start<=end&&str.charAt(start)==' '){
start++;
}
//结尾的空格
while(start<=end&&str.charAt(end)==' '){
end--;
}
return str.substring(start,end+1);//截取

}

public staticvoid sop(Object obj){
System.out.println(obj);
}

}


Hello Java!
Hello Java!


[b]1.9、字符串反转[/b]

package com.pb.string.demo2;
/**
* 将一个字符串返转
* @author denny
*思路:
*可以将字符串变成数组,对数组反转
*将反转后的数组,转换成字符串
*只要将或者反转的部分的开始和结束位置作为参数传递即可
*/
public class StringDemo2 {

public staticvoid main(String[] args) {

String str="abcdef";
sop(str);
str=reverseString(str,1,5);
System.out.println(str);
}
//从某个位置开始反转到某个位置结束
public static String reverseString(String s,int start,int end){
char [] ch=s.toCharArray();
reverse(ch,start,end);
return new String(ch);

}
//方法重载,反转全部
public static String reverseString(String s){

return reverseString(s,0,s.length());

}
//反转 参数,开始位到结束位置,但不包含结束位
private staticvoid reverse(char [] arr,int x,int y){
for(int start=x,end=y-1;start<end;start++,end--){
swap(arr,start,end); //交换值

}
}
//交换
private staticvoid swap(char [] arr,int start,int end){
char tmp=arr[start];
arr[start]=arr[end];
arr[end]=tmp;
}

public staticvoid sop(Object obj){
System.out.println(obj);
}

}


abcdef
aedcbf


1.10、获取字符串在另一个字符串中出现的次数

package com.pb.string.demo2;

/**
* 获取字符串在另一个字符串中出现的次数
*
* @author Denny 定义一个计数器 获取第一次出现的位置 从第一次出现的位置后,剩余的字符串中继续获取再现的位置 每获取一次就加1
* 当获取不到时计数完成
*/
public class StringDemo3 {

public staticvoid main(String[] args) {
String s1 = "kkabkkcdkkefkkskk";
String s2 = "kk";

int count = getCount_2(s1, s2);

System.out.println("count="+count);

}

public staticint getCount(String s1, String key) {
int count = 0;
int index=0;//下标

while((index=s1.indexOf(key))!=-1){
sop("index="+index);
sop("s1="+s1);
s1=s1.substring(index+key.length()); //截取字符串,再赋值给字符串
count++;
}
return count;
}
public staticint getCount_2(String s1, String key) {
int count = 0;
int index=0;//下标

while((index=s1.indexOf(key,index))!=-1){

index=index+key.length();
sop("index="+index);
count++;
}
return count;
}
public staticvoid sop(Object obj){
System.out.println(obj);
}
}


结果:

index=2
index=6
index=10
index=14
index=17
count=5


1.11、获取2个字符串最大相同的串

package com.pb.string.demo2;

/**
*
* @author Denny 获取2个字符串最大相同的串, 将短的字符进行一长度递减的子串打印 将知的字符串按照长度弟减的方式获取到 如果包含就找到!
*/
public class StringDemo4 {

public staticvoid main(String[] args) {
String s1 = "abcwerthelloyuiodef";
String s2 = "cvhelloybnm";
sop("最大相同的是:"+getMaxString(s2,s1));
}

public static String getMaxString(String s1, String s2) {
String max = "", min = "";
//判断哪个字符串长
max = (s1.length() > s2.length()) ? s1 : s2;
min=(max==s1)?s2:s1;
//System.out.println("max="+max+",min="+min);
//使用小的字符串做循环
for (int i = 0; i < min.length(); i++) {
//每次都从0开始,从后减少
for(int y=0,z=min.length()-i;z!=min.length()+1;y++,z++){
String temp=min.substring(y,z);
sop(temp);
if(max.contains(temp)){
return temp;
}
}
}
return "";

}
public staticvoid sop(Object obj){
System.out.println(obj);
}

}


cvhelloybnm
cvhelloybn
vhelloybnm
cvhelloyb
vhelloybn
helloybnm
cvhelloy
vhelloyb
helloybn
elloybnm
cvhello
vhelloy
helloyb
elloybn
lloybnm
cvhell
vhello
helloy
最大相同的是:helloy


[b]二、StringBuffer[/b]

2.1、StringBuffer

首先StringBuffer是字符串缓冲区,是一个容器。

每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组。如果内部缓冲区溢出,则此容量自动增大。从 JDK 5 开始,为该类补充了一个单个线程使用的等价类,即
StringBuilder
。与该类相比,通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但由于它不执行同步,所以速度更快。

1.长度是可以变化的

2.可以操作多个数据类型

3.最终会通过toString()方法变成字符串.

容器:增加,删除,改,查

c----create append()将指定字符串,添加到现在的字符串末尾

u----update

r-----read

d----delete

append()将指定字符串,添加到现在的字符串末尾

insert插入到指定位置append(索引,要插入的字符串或者其它类型)

delete 删除

2.2、示例

添加,插入

package com.pb.stringbuffer.demo1;

public class StringBufferDemo1 {

public staticvoid main(String[] args) {
StringBuffer sb=new StringBuffer();
sb.append("abcd").append(123).append("mmmmmmmm");
//插入
sb.insert(2, "qqqqqqqqqqq");
System.out.println(sb);

}

}


删除 包含start不包含end

StringBuffer
delete(int start,int end)

移除此序列的子字符串中的字符。
StringBuffer
deleteCharAt(int index)


移除此序列指定位置的
char
package com.pb.stringbuffer.demo1;

public class StringBufferDemo2 {

public staticvoid main(String[] args) {

getChar();
}
//获取指定字符串,并存放在字符数组中
public staticvoid getChar(){
StringBuffer sb=new StringBuffer("abcdef");
char [] ch=newchar[8];
sb.getChars(1, 4, ch, 2);
for (int i = 0; i < ch.length; i++) {
sop("ch["+i+"]="+ch[i]);
}

}

//修改
public staticvoid update(){
StringBuffer sb=new StringBuffer("abcdef");
sb.replace(1, 4, "33333333333");
sop(sb);
//替换一个
sb.setCharAt(1, 'k');
sop(sb);
}

public staticvoid del(){
StringBuffer sb=new StringBuffer("abcdefghi");
sb.delete(1, 3);
sop(sb);
//删除一个

sb.deleteCharAt(sb.indexOf("i"));
sop(sb);
//清空缓冲区
sb.delete(0, sb.length());
sop(sb);
}
//添加,插入
public staticvoid add_insert(){
StringBuffer sb=new StringBuffer();
sb.append("abcd").append(123).append("mmmmmmmm");
//插入
sb.insert(2, "qqqqqqqqqqq");
System.out.println(sb);
}

public staticvoid sop(Object obj){
System.out.println(obj);
}
}


2.3、常用方法

构造方法摘要
StringBuffer()

构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
StringBuffer(CharSequence seq)


public java.lang.StringBuilder(CharSequence seq)
构造一个字符串缓冲区,它包含与指定的
CharSequence
相同的字符。
StringBuffer(int capacity)


构造一个不带字符,但具有指定初始容量的字符串缓冲区。
StringBuffer(String str)


构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
方法摘要
StringBuffer
append(boolean b)


boolean
参数的字符串表示形式追加到序列。
StringBuffer
append(char c)


char
参数的字符串表示形式追加到此序列。
StringBuffer
append(char[] str)


char
数组参数的字符串表示形式追加到此序列。
StringBuffer
append(char[] str,
int offset,int len)

char

数组参数的子数组的字符串表示形式追加到此序列。
StringBuffer
append(CharSequence s)


将指定的
CharSequence
追加到该序列。
StringBuffer
append(CharSequence s,int start,
int end)

将指定
CharSequence

的子序列追加到此序列。
StringBuffer
append(double d)


double
参数的字符串表示形式追加到此序列。
StringBuffer
append(float f)


float
参数的字符串表示形式追加到此序列。
StringBuffer
append(int i)


int
参数的字符串表示形式追加到此序列。
StringBuffer
append(long lng)


long
参数的字符串表示形式追加到此序列。
StringBuffer
append(Object obj)


追加
Object
参数的字符串表示形式。
StringBuffer
append(String str)


将指定的字符串追加到此字符序列。
StringBuffer
append(StringBuffer sb)


将指定的 StringBuffer 追加到此序列中。
StringBuffer
appendCodePoint(int codePoint)


codePoint
参数的字符串表示形式追加到此序列。
int
capacity()


返回当前容量。
char
charAt(int index)


返回此序列中指定索引处的
char
值。
int
codePointAt(int index)


返回指定索引处的字符(统一代码点)。
int
codePointBefore(int index)


返回指定索引前的字符(统一代码点)。
int
codePointCount(int beginIndex,
int endIndex)

返回此序列指定文本范围内的统一代码点。
StringBuffer
delete(int start,
int end)

移除此序列的子字符串中的字符。
StringBuffer
deleteCharAt(int index)


移除此序列指定位置的
char
void
ensureCapacity(int minimumCapacity)


确保容量至少等于指定的最小值。
void
getChars(int srcBegin,
int srcEnd,char[] dst,int dstBegin)

将字符从此序列复制到目标字符数组
dst
int
indexOf(String str)


返回第一次出现的指定子字符串在该字符串中的索引。
int
indexOf(String str,
int fromIndex)

从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
StringBuffer
insert(int offset,
boolean b)

boolean

参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
char c)

char
参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
char[] str)

char

数组参数的字符串表示形式插入此序列中。
StringBuffer
insert(int index,
char[] str,int offset,int len)

将数组参数
str

的子数组的字符串表示形式插入此序列中。
StringBuffer
insert(int dstOffset,
CharSequence s)


将指定
CharSequence
插入此序列中。
StringBuffer
insert(int dstOffset,
CharSequence s,int start,
int end)

将指定
CharSequence

的子序列插入此序列中。
StringBuffer
insert(int offset,
double d)

double
参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
float f)

float
参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
int i)

int
参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
long l)

long
参数的字符串表示形式插入此序列中。
StringBuffer
insert(int offset,
Object obj)


Object
参数的字符串表示形式插入此字符序列中。
StringBuffer
insert(int offset,
String str)


将字符串插入此字符序列中。
int
lastIndexOf(String str)


返回最右边出现的指定子字符串在此字符串中的索引。
int
lastIndexOf(String str,
int fromIndex)

返回最后一次出现的指定子字符串在此字符串中的索引。
int
length()


返回长度(字符数)。
int
offsetByCodePoints(int index,
int codePointOffset)

返回此序列中的一个索引,该索引是从给定
index

偏移
codePointOffset
个代码点后得到的。
StringBuffer
replace(int start,
int end,String str)

使用给定
String
中的字符替换此序列的子字符串中的字符。
StringBuffer
reverse()


将此字符序列用其反转形式取代。
void
setCharAt(int index,
char ch)

将给定索引处的字符设置为
ch
void
setLength(int newLength)


设置字符序列的长度。
CharSequence
subSequence(int start,
int end)

返回一个新的字符序列,该字符序列是此序列的子序列。
String
substring(int start)


返回一个新的
String
,它包含此字符序列当前所包含的字符子序列。
String
substring(int start,
int end)

返回一个新的
String
,它包含此序列当前所包含的字符子序列。
String
toString()


返回此序列中数据的字符串表示形式。
void
trimToSize()


尝试减少用于字符序列的存储空间。

[b]三、StringBuilder[/b]

3.1、StringBuilder简述

用法与StringBuffer一样

StringBuilder
的实例用于多个线程是不安全的。如果需要这样的同步,则建议使用
StringBuffer


从 JDK 5 开始,为该类补充了一个单个线程使用的等价类,即
StringBuilder
。与该类相比,通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但由于它不执行同步,所以速度更快。

3.2、String、StringBuffer和StringBuilder区别

String 长度是固定不变的

StringBuffer和StringBuilder是容器,长度是可变

StringBuffer是同步的是线程安全的。效率低 。多线程

StringBuilder是没有同步的,非线程安全的,效率高. 单线程,或者手动加锁 也可以用于多线程

升级:提高效率,提高安全,简化写代码效率。建议使用StringBuilder

四、包装类

4.1、基本数据类型对应的包装类

byte Byte

short   Short

int     Interget

long    Long

boolean Boolean

float    Float

double Double

char    Character

4.2、以int为类

字段摘要
staticint
MAX_VALUE

值为 231-1 的常量,它表示
int
类型能够表示的最大值。
staticint
MIN_VALUE


值为 -231 的常量,它表示
int
类型能够表示的最小值。
staticint
SIZE


用来以二进制补码形式表示int 值的比特位数。
static Class<Integer>
TYPE


表示基本类型
int
Class

实例。
构造方法摘要
Integer(int value)


构造一个新分配的
Integer
对象,它表示指定的
int

值。
Integer(String s)


构造一个新分配的
Integer
对象,它表示
String
参数所指示的
int
值。
package com.pb.demo.packclass.demo1;

public class Interger {

public staticvoid main(String[] args) {

Integer max=Integer.MAX_VALUE;//最大值
Integer min=Integer.MIN_VALUE;//最小值
System.out.println("max="+max);
System.out.println("min="+min);

}

}


常用操作

用于基本数据类型和字符串类型之间做转换

4.3、基本数据类型转换成字符串

基本数据类型+"";

toString(基本数据类型);

String.ValueOf(基本数据类型)

package com.pb.demo.packclass.demo1;
/**
* 转换为字符串
* @author Denny
*
*/
public class Demo1 {

public staticvoid main(String[] args) {

double d=33.4;
String s1=d+"";
String s2=Double.toString(d);
String s3=String.valueOf(d);

}

}


4.4、字符串转换成基本数据类型

基本数据类型 值=基本类型类型的包装类.parseXxx(String);

int a=Integer.parseInt("123");

package com.pb.demo.packclass.demo1;

public class Demo2 {

public staticvoid main(String[] args) {
String s1="32";
int i=Integer.parseInt(s1);//转换为int类型
String s2="22.4";
double d=Double.parseDouble(s2);//转换为double
String s3="11.3";
float f=Float.parseFloat(s3);//转换为float类型
long l=Long.parseLong("88888888");
boolean b=Boolean.parseBoolean("true");//或者false
Integer x=new Integer(33);
int num=x.intValue(); //动态方法 转换为基本数据类型

}

}


4.5、进制转换

package com.pb.demo.packclass.demo1;

public class Demo3 {

public staticvoid main(String[] args) {
//转换为二进制
System.out.println(Integer.toBinaryString(10));
//八进制
System.out.println(Integer.toOctalString(10));
//十六进制
System.out.println(Integer.toHexString(10));
//转换为2进制
System.out.println(Integer.parseInt("110", 2));
//转换为8进制
System.out.println(Integer.parseInt("17", 8));
//转换为16进制
System.out.println(Integer.parseInt("af", 16));
}

}


结果:

1010
12
a
6
15
175


4.6、新特性

自动装箱和自动拆箱

package com.pb.demo.packclass.demo1;

public class Demo4 {

public staticvoid main(String[] args) {

Integer x=4;// 自动装箱,==new Integer(4),
x=x+2;//自对拆箱 x.intValue()转换为int类型,+2运算,再进行自动装箱
}

public staticvoid sop(Object obj){
System.out.println(obj);
}
}


package com.pb.demo.packclass.demo1;

public class Demo5 {

public staticvoid main(String[] args) {
Integer i=128;
Integer j=128;
sop("i==j: "+(i==j));//false

/*
* 如果指向同一个Integr对象
* 当数值在byte范围内里-128-127之间
* 1.5后的新特性
* 如果该值已经存在,就不会开辟新的空间
*/

Integer x=127;
Integer y=127;
sop("x==y:"+(x==y));//true

}
public staticvoid sop(Object obj){
System.out.println(obj);
}
}


int
compareTo(Integer anotherInteger)

在数字上比较两个
Integer
对象。
package com.pb.demo.packclass.demo1;

public class Demo6 {

public staticvoid main(String[] args) {
Integer x=3;
Integer y=8;
sop(x.compareTo(y)); //-1 小于
sop(Integer.compare(y, x));//大于1
sop(Integer.compare(5, 5));//等于 0

}
public staticvoid sop(Object obj){
System.out.println(obj);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: