文档库 最新最全的文档下载
当前位置:文档库 › java字符串

java字符串

java字符串:KA0346-5368141

String s=new String("We are students");

String s="We are students";

利用字节数组生成字符串:

byte cDem0l[]={66,67,68};

byte cDem02[]={65,66,67,68};

String strDem01=new String(cDem0l);

String strDem02=new String(cDem02,1,3);

利用上面的两个构造方法生成的字符串实例的内容均为"BCD"

字符串比较函数:

equals,equalsIgnoreCase

String tom =new String(“ABC”),

Jerry=new String(“abc”);

tom.equalsIgnoreCase(Jerry)的值是true。

startsWith、endsWith

String tom= "220302*********",jerry= "21079670924022";

tom.startsWith("220")的值是true;tom.endsWith("021")的值是true regionMatches:从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other 指定的一个子串进行比较。

compareTo,compareToIgnoreCase:按字典顺序与参数s 指定的字符串比较大小

String str= "abcde";

https://www.wendangku.net/doc/2817531518.html,pareTo("boy"); //小于0

例:String a[]={"Java","Basic","C++","Fortran","SmallTalk"};

for(int i=0;i

for(int j=i+1;j

if(a[j].compareTo(a[i])<0){

String temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

6.其他命令

(1) 字符串大小写转换,申明格式如下:

public String toUpperCase(Locale locale) //仅对指定位置进行转换

public String toUpperCase()

public String toLowerCase(Locale locale) //仅对指定位置进行转换

public String toLowerCase()

(2) 转换为字符串数组,申明格式如下:

public char[ ] toCharArray( )

(3) 字符串到字符数组之间的转换,申明格式如下:

getChars(int srcBegin,int srcEnd,char[ ] dst,int dstBegin)

(4) 连接两个字符串,申明格式如下:

public String concat(String str)

该方法用于将两个字符串连接在一起,与字符串的“+”操作符功能相同。

StringBuffer类

添加操作append()

StringBuffer sbfSource=new StringBuffer("1+2=”);

int nThree=3;

sbfSource.append(nThree);

System.out.println(sbfSource.toString( ));

输出结果为: 1+2=3

插入操作insert()

StringBuffer sbfSource=new StringBuffer("1+=2");

int nOne=1;

sbfSource.insert(2,nOne);

System.out.println(sbfSource.toString());

输出结果为:1+1=2

字符串缓冲区与字符串之间的转换toString()

将字符串缓冲区转换为字符串,该方法返回类型为字符串。该方法是从缓冲区字符串向字符串转换的方法,十分重要。

取字符

(1) charAt(int index)

取得指定位置的字符。返回值类型为字符char。位置编号从0开始。

下面的代码段为charAt方法的例子:

StringBuffer sbfSource=new StringBuffer(10);

sbfSource.append("My");

char c=sbfSource.charAt(0);// ’M’

(2) getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)

赋值指定位置的字符到字符串数组dst. 无返回值。

下面的代码段为getChars方法的例子:

StringBuffer sbfSource=new StringBuffer("You are the best!");

char[] str;

sbfSource.getChars(0,2,str,0);// “Yo”

字符串反转

public StringBuffer reverse()

获取长度

StringBuffer sbfSource=new StringBuffer(10);

SbfSource.append("you");

System.out.println("字符串缓冲区的剩余长度为:”+sbfSource.capacity());

输出结果为:字符串缓冲区的剩余长度为:10

StringBuffer sbfSource=new StringBuffer(10);

SbfSource.append("you");

System.out.println(“字符串缓冲区的长度为:”+sbfSource.length( ));

输出结果为:

字符串缓冲区的长度为:3

自己编写字符串反转函数

public class Reverse {

public static void main(String args[]){

String strSource = new String(“I love Java”);

String strDest = reverseIt ( strSource );

System.out.println(strDest);

}

public static String reverseIt(String source) {

int i, len = source.length();

StringBuffer dest = new StringBuffer(len);

for (i = (len - 1); i >= 0; i--)

dest.append(source.charAt(i));

return dest.toString();

}

}

程序运行结果如下:

avaJ evol I

StringTokenizer

自定义分割符:StringTokenizer fenxi=new StringTokenizer("we ,are ; student", ", ; ");

求字符串中的当前单词的数量 countTokens:

String str=new String("I love Java");

StringTokenizer st=new StringTokenizer(str);

int nTokens=st.countTokens(); // 值为3

例:

public class TestToken

{

public static void main(String args[])

{

//构造StringTokenizer对象

StringTokenizer st = new StringTokenizer("this is a Java programming");

//在字符串中匹配默认的分隔符

while(st.hasMoreTokens())

{

//打印当前分隔符和下一分隔符之间的内容

System.out.println(st.nextToken());

}

}

}

程序运行结果如下

this

is

a

Java

Programming

将简单数据转换成字符串

public class Test2 { int--new Integer()-->Integer--toString()-->String

public static void main(String args[]) {

int nInt = 10;

float fFloat = 3.14f;

double dDouble = 3.1415926;

Integer obj1 = new Integer(nInt); // 转换为整型

Float obj2 = new Float(fFloat); // 转换为浮点数类型

Double obj3 = new Double(dDouble); // 转换为双精度类型

String strString1 = obj1.toString();// 分别调用toString方法转换为字符串

System.out.println(strString1);

String strString2 = obj2.toString();

System.out.println(strString2);

String strString3 = obj3.toString();

System.out.println(strString3);

}

}

public class Test2 {

public static void main(String args[]) {

Calendar c = Calendar.getInstance();

c.setTime(new Date());

String y = String.valueOf(c.get(Calendar.YEAR));

String m = String.valueOf(c.get(Calendar.MONTH) + 1);

System.out.println("现在的时间是:");

System.out.println(y + "年" + m + "月");

System.out.print(c.get(Calendar.YEAR));

System.out.print(c.get(Calendar.MONTH) + 1);

}

}

同时,类Integer、Double、Float和Long中也提供了静态的类方法valueOf( )把一个字符串转

化为对应的数字对象类型。String--Integer.valueOf()-->Integer--intValue()-->int public static Double valueOf(String s) throws NumberFormatException String strPI=”3.1415926”;

Double dpi=Double.valueOf(strPI);

double ddPI=dpi.doubleValue( );

float ffPI=dpi.floatValue();

同时Boolean、Byte、Double、Float、Integer、Long等类也分别提供了静态方法parseDouble(String)、 parseFloat(String)、parseInt(String)、 parseLong(String) 等方法将字符串对象转换成其他简单数据类型的方法。String--Integer.parseInt()-->int

将字符串转换为相应的简单数据类型

public class Test2 {

public static void main(String args[]) {

char[] cArray;

int nInt;

float fFloat;

double dDouble;

String strString = new String("I love Java");

String strInteger = new String("314");

String strFloat = new String("3.14");

String strDouble = new String("3.1416");

cArray = strString.toCharArray();

System.out.println(cArray);

nInt = Integer.parseInt(strInteger);

System.out.println(nInt);

fFloat = Float.parseFloat(strFloat);

System.out.println(fFloat);

dDouble = Double.parseDouble(strDouble);

System.out.println(dDouble);

}

}

相关文档