服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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类的对

扫描关注微信公众号