您的位置:首页 > Web前端

Java中的String、StringBuffer和Math类

2008-11-23 19:28 281 查看
本篇文章将介绍一下Java的一些主要类,String类、StringBuffer类、Math类。

Java是一种真正的面向对象的语言,即使是开发简单的程序,也必须设计对象。Java自身也为我们提供了许多已设计好的类,要想灵活使用Java进行编程,熟悉Java的这些主要类将是必不可少的前提条件之一。

String 类

顾名思义,String是串的意思,这个类是字符串常量的类。相信使用过C语言进行编程的人都知道字符串是怎么回事,这里就不再进行赘述了。但有一点要说
明的是,Java中的字符串和C语言中的字符串是有区别的。在C语言中,并没有真正意义上的字符串,C语言中的字符串就是字符数组,使用起来非常的灵活。
而在Java中,字符串常量是一个类??String类,它和字符数组是不同的。

下面先介绍一下String类的构造函数

public String()



这个构造函数用来创建一个空的字符串常量。



如:String test=new String();

或:String test;

test=new String();



public String(String value )



这个构造函数用一个已经存在的字符串常量作为参数来创建一个新的字符串常量。

另外值得注意的是,Java会为每个用双引号"......"括起来的字符串常量创建一个String类的对象。如:String k="Hi."; Java会为"Hi." 创建一个String类的对象,然后把这个对象赋值给k。等同于:

String temp=new String("Hi.");

String k=temp;



这个构造函数的用法如:

String test=new String(k); (注:k是一个String类的对象)

String test=new String("Hello, world.");



public String( char value[] )



这个构造函数用一个字符数组作为参数来创建一个新的字符串常量。

用法如:

char z[]={'h','e','l','l','o'};

String test=new String(z);

(注:此时test中的内容为"hello")



public String( char value[], int offset, int count )



这个构造函数是对上一个的扩充,用一句话来说,就是用字符数组value,从第offset个字符起取count个字符来创建一个String类的对象。用法如:

char z[]={'h','e','l','l','o'};

String test=new String(z,1,3);

(注:此时test中的内容为"ell")

注意:数组中,下标0表示第一个元素,1表示第二个元素……

如果 起始点offset 或 截取数量count 越界,将会产生异常

StringIndexOutOfBoundsException



public String( StringBuffer buffer )



这个构造函数用一个StringBuffer类的对象作为参数来创建一个新的字符串常量。

String类是字符串常量,而StringBuffer类是字符串变量,是不同的。StringBuffer类将在后面进行介绍。

String类的方法有:

public char charAt( int index )



这个方法用来获取字符串常量中的一个字符。参数index指定从字符串中返回第几个字符,这个方法返回一个字符型变量。用法如:

String s="hello";

char k=s.charAt(0);

(注:此时k的值为'h')



public int compareTo( String anotherString )



这个方法用来比较字符串常量的大小,参数anotherString为另一个字符串常量。若两个字符串常量一样,返回值为0。若当前字符串常量大,则返回值大于0。若另一个字符串常量大,则返回值小于0。用法如:

String s1="abc";

String s2="abd";

int result=s2.compareTo(s1);

(注:result的值大于0,因为d在ascII码中排在c的后面,则s2>s1)



public String concat( String str )



这个方法将把参数??字符串常量str 接在当前字符串常量的后面,生成一个新的字符串常量,并返回。用法如:

String s1="How do ";

String s2="you do?";

String ss=s1.concat(s2);

(注:ss的值为"How do you do?")



public boolean startsWith( String prefix )



这个方法判断当前字符串常量是不是以参数??prefix字符串常量开头的。是,返回true。否,返回false。 用法如:

String s1="abcdefg";

String s2="bc";

boolean result=s1.startsWith(s2);

(注:result的值为false)



public boolean startsWith( String prefix, int toffset )



这个重载方法新增的参数toffset指定 进行查找的起始点。

public boolean endsWith( String suffix )



这个方法判断当前字符串常量是不是以参数??suffix字符串常量结尾的。是,返回true。否,返回false。用法如:

String s1="abcdefg";

String s2="fg";

boolean result=s1.endsWith(s2);

(注:result的值为true)



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



这个方法用来从字符串常量中截取一段字符串并转换为字符数组。

参数srcBegin为截取的起始点,srcEnd为截取的结束点,dst为目标字符数组,dstBegin指定将截取的字符串放在字符数组的什么位置。实际上,srcEnd为截取的结束点加1,srcEnd-srcBegin为要截取的字符数,用法如:

String s="abcdefg";

char z[]=new char[10];

s.getChars(2,4,z,0);

(注:z[0]的值为'c',z[1]的值为'd',截取了两个字符 4-2=2)



public int indexOf( int ch )



这个方法的返回值为 字符ch在字符串常量中从左到右第一次出现的位置。若字符串常量中没有该字符,则返回-1。用法如:

String s="abcdefg";

int r1=s.indexOf('c');

int r2=s.indexOf('x');

(注:r1的值为2,r2的值为-1)



public int indexOf( int ch, int fromIndex )



这个方法是对上一个方法的重载,新增的参数fromIndex为查找的起始点。用法如:

String s="abcdaefg";

int r=s.indexOf('a',1);

(注:r的值为4)



public int indexOf( String str )



这个重载方法返回 字符串常量str在当前字符串常量中从左到右第一次出现的位置,若当前字符串常量中不包含字符串常量str,则返回-1。用法如:

String s="abcdefg";

int r1=s.indexOf("cd");

int r2=s.indexOf("ca");

(注:r1的值为2,r2的值为-1)



public int indexOf( String str, int fromIndex )



这个重载方法新增的参数fromIndex为查找的起始点。

 

以下四个方法与上面的四个方法用法类似,只是在字符串常量中从右向左进行查找。

public int lastIndexOf( int ch )



public int lastIndexOf( int ch, int fromIndex )



public int lastIndexOf( String str )



public int lastIndexOf( String str, int fromIndex )



public int length()



这个方法返回字符串常量的长度,这是最常用的一个方法。用法如:

String s="abc";

int result=s.length();

(注:result的值为3)



public char[] toCharArray()



这个方法将当前字符串常量转换为字符数组,并返回。用法如:

String s="Who are you?";

char z[]=s.toCharArray();



public static String valueOf( boolean b )



public static String valueOf( char c )



public static String valueOf( int i )



public static String valueOf( long l )



public static String valueOf( float f )



public static String valueOf( double d )



以上6个方法可将boolean、char、int、long、float和double 6种类型的变量转换为String类的对象。用法如:

String r1=String.valueOf(true); (注:r1的值为"true")

String r2=String.valueOf('c'); (注:r2的值为"c")

float ff=3.1415926;

String r3=String.valueOf(ff); (注:r3的值为"3.1415926")



 

StringBuffer 类

String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的。

String类的构造函数

public StringBuffer()



创建一个空的StringBuffer类的对象。

public StringBuffer( int length )



创建一个长度为 参数length 的StringBuffer类的对象。

注意:如果参数length小于0,将触发NegativeArraySizeException异常。

public StringBuffer( String str )



用一个已存在的字符串常量来创建StringBuffer类的对象。

StringBuffer类的方法有:

public String toString()



转换为String类对象并返回。由于大多数类中关于显示的方法的参数多为String类的对象,所以经常要将StringBuffer类的对象转换为String类的对象,再将它的值显示出来。用法如:

StringBuffer sb=new StringBuffer("How are you?");

Label l1=new Label(sb.toString());

(注:声明一个标签对象l1,l1上的内容为How are you?)



public StringBuffer append( boolean b )



public StringBuffer append( char c )

public StringBuffer append( int i)

public StringBuffer append( long l )



public StringBuffer append( float f )



public StringBuffer append( double d )



以上6个方法可将boolean、char、int、long、float和double 6种类型的变量追加到StringBuffer类的对象的后面。用法如:

double d=123.4567;

StringBuffer sb=new StringBuffer();

sb.append(true);

sb.append('c').append(d).append(99);

(注:sb的值为truec123.456799)



public StringBuffer append( String str )



将字符串常量str追加到StringBuffer类的对象的后面。

public StringBuffer append( char str[] )



将字符数组str追加到StringBuffer类的对象的后面。

public StringBuffer append( char str[], int offset, int len )



将字符数组str,从第offset个开始取len个字符,追加到StringBuffer类的对象的后面。

 

public StringBuffer insert( int offset, boolean b )



public StringBuffer insert( int offset, char c )



public StringBuffer insert( int offset, int i )



public StringBuffer insert( int offset, long l )



public StringBuffer insert( int offset, float f )



public StringBuffer insert( int offset, double d )



public StringBuffer insert( int offset, String str )



public StringBuffer insert( int offset, char str[] )



将boolean、char、int、long、float、double类型的变量、String类的对象或字符数组插入到StringBuffer类的对象中的第offset个位置。用法如:

StringBuffer sb=new StringBuffer("abfg");

sb.insert(2,"cde");

(注:sb的值为abcdefg)



public int length()



这个方法返回字符串变量的长度,用法与String类的length方法类似。

 

Math 类

数学类包含了许多数学函数,如sin、cos、exp、abs等。Math类是一个工具类,它在解决与数学有关的一些问题是有着非常重要的作用。

这个类有两个静态属性:E和PI。E代表数学中的e 2.7182818,而PI代表派pi 3.1415926。

引用时,用法如:Math.E 和 Math.Pi

这个类的方法有:

public static int abs( int a )



public static long abs( long a )



public static float abs( float a )



public static double abs( double a )



abs方法用来求绝对值。

public static native double acos( double a )



acos求反余弦函数。

public static native double asin( double a )



asin求反正弦函数。

public static native double atan( double a )



atan求反正切函数。

public static native double ceil( double a )



ceil返回 最小的 大于a的整数。

public static native double cos( double a )



cos求余弦函数。

public static native double exp( double a )



exp求e的a次幂。

public static native double floor( double a )



floor返回 最大的 小于a的整数。

public static native double log( double a )



log返回lna。

public static native double pow( double a, double b )



pow求a的b次幂。

public static native double sin( double a )



sin求正弦函数。

public static native double sqrt( double a )



sqrt求a的开平方。

public static native double tan( double a )



tan求正切函数。

public static synchronized double random()



返回0到1之间的随机数。

使用这些方法时,用法为Math.***** (*****为方法名)。用法如:

int a=Math.abs(124);

int b=Math.floor(-5.2);

double s=Math.sqrt(7);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: