第六章 常用类总结


 

 

字符串相关类(string stringbuffer

基本数据类型包装类

Math

File

枚举类

 

 

 string

Java.lang.string 类代表   不可变的字符序列。

xxxxxxx”为该类的一个对象

String类的常见构造方法:

String(String  original)

    创建一个String 对象为original 的拷贝

String char[] value

    用一个字符数组创建一个string的 对象

String (char[] value.int offset.int count)

     用一个字符数组从offset项开始的count个字符序列 创建一个string对象

(再次复习)

Java.lang.string 类代表 不可变的 字符序列

xxxxxxx”为该类的一个对象。

String类常见的构造方法

       String(string(original))

            创建一个string 对象为original 的拷贝

      String(char[] value)

             用一个字符组创建一个string对象

      Stringchar[]  value,int offset,int count

            用一个字符组从offset项开始的count个字符序列创建一个string序列

//复习结束

 

      

类举例代码如下

Public Class Test{

   Public static void main(string[] args){

      String s1=”hello”;string s2=”world”;

      String s3=”hello”;

      System.out.println(s1==s3);//true

      

      S1=new string(“hello”);

      S2=new sreing(‘hello”);

      System.out.println(s1==s2);//false

      System.out.println(s1.equals(s2));//true

  

      Char c[]={‘s’,’u’,’n’,’j’,’a’,’v’,’a’};

      String s4=new string(c);

      String s5=new string(c,4,4);

      System.out.println(s4);//sun java

      System.out.println(s5);//java

}

}

      

(复习一遍)

Public class test

  Public static void mainstring[] args{

      String  s1=”hello”;string  s2=”world”;

      String  s3=”hello”;

      System.out.println(s1=s3);//true

   

       S1=new string(“hello”);

       S2=new string(“hello”);

       System.out.println(s1==s2);//false

       System.out.println(s1.equals(s2));//true

     

       Char c[]={‘s’,’u’,’n’,’j’,’a’,’v’,’a’};

       String  s4=new string(c);

       String  s5=new string(c,4,4);

       System.out.println(s4);//sun java

       System.out.println(s5);//java

 

(复习完毕)

 

 

 

 

 

  public 类常用方法(1

 

Public char charAt(int index)

返回字符串第index个字符

Public int length()

返回字符串的长度

Public int indexofstring str

返回字符串中出现str的第一个位置

Public int indexofstring strintfromindex

返回字符串中从fromindex 开始出现str的 第一个位置

(复习一遍)

   Public  char  charAt(int index)

   返回字符串中 index个字符;

Public int length()

返回字符串的 长度、;

Public int indexof(string str)

返回字符串 出现str的 第一个位置

Public intindexof(string str,int fromindex)

返回字符串中从fromindex开始出现str的 第一个位置

(继续开始)

public boolean  equalsignorecase(string another)

比较字符串和another是否一样 (忽略大小写)

Public string replace(char oldchar,char newChar)

在字符串中 newchar字符替换oldchar字符。

(再次全部复习一遍)

Public char charAt(int index)

 

返回字符串中第index个字符

 

Public int length()

返回字符串的 长度

Public int indexof(string str)

Public int indexof9string str)

返回字符串出现str 的第一个位置

Public  int  indexof (string str,int fromindex)

Public  int  indexof(string str,int fromindex)

返回字符串 fromindex开始到出现str的 第一个位置)

Public Boolean equalsignorecase(string another)

Public boolean equalsignorecase(string another)

Public Boolean equalsignorecase(string another)

比较和字符串another 是否完全一样(忽略大小写)

Public string replace(char oldchar,char newchar)

Public string replace(char oldchar,char newchar)

在字符串里面用newchar 字符代替oldchar字符

  

 

 

 

String类举例(2)

 

Public class test{

  Public static void main(string[] args){

      System.out.println(s1.charAt(1));//u

      System.out.println(s2.length());//8

                    System.out.println(s1.indexof(”java”));//4

System.out.println(s1.indexof(“java”));//-1

System.out.println(s1.equals(s2));//false

System.out.println(s1.equalsignorecase(s2));

//true

String s=”我是程序员,我在学Java”;

String sr=s.replace(‘ ‘,’你’);

System.out.println(sr);

//你是程序员,你在学Java

}

}

 

(复习一遍)

Public class test

  Public static void main(string[] args){

String s1=”sun java”,s2=”Sun Java”;

System.out.println(s1.charAt(1));//u

System.out.println(s2.length());//8

System.out.println(s1.indexof(“java”));//4

(再来一遍)

Public class test

  Public static void main(string[] args){

    String s1=“sun java”,s2=”Sun Java”;

    System.out.println(s1.charAt(1));//u

//为什么是 u  ,有点疑问

    System.out.println(s1.indexof(“java”));//4

//wwhy      the  second  question.

 

System.out.println(s1.indexof(“Java”));//-1

System.out.println(s1.equals(s2));//false

System.out.println(s1.equalsignorecase(s2));//true

 

String s=”我是程序员,我在学java”;

String sr=s.replace(‘我’,‘你’);

//sr 是什么意思?第三个问题

System.out.println(sr);

//你是程序员,你在学java

String 类常用方法(二)

Public Boolean startwith(string prefix)

判断字符串是否以 prefix开头

Public Boolean startwith(string prefix)

判断 字符串是否以 prefix开头

Public Boolean endwith(string  suffix)

------------------suffix结尾

Public string touppercase()

返回一个字符串为该字符串的大写形式

Public string touppercase()

返回一个字符串为该字符串的大写形式

Public string tolowercase();

返回一个字符串为该字符串的小写形式

Public string substring(int beginindex)

Public string substring(int beginindex)

返回该字符串从beginindex开始到结尾的子字符串

Public string substring(int beginindex,int endindex)

Public string substring(int beginindex,int

Endindex)

返回到该字符串从beginindex到endindex结尾的字符串

Public string trim()

Public string stim()

 返回到该字符串去掉开头和结尾空格后的 字符串

返回到该字符串去掉开头和结尾空格的 字符串

(review   一遍)

Public Boolean startwith(string prefix)

Public boolean startwith(string prefix)

判断 字符串是否以 prefix字符串开头

Public boolean endwith(string suffix)

判断字符串是否以prefix 字符结尾

Public string touppercase()

返回到一个字符串为该字符串的大写形式

Public string tolowercase()

返回 该字符串 为小写形式

Public string substring(int beginindex)

返回该字符串 beginindex开始到结尾的子字符串

 

 

 

 

实例代码

Public class  test{

  Public  static void main (string [] args){

      String s=”welcome to java  world

 

 

 

 

 

 

String类常用方法(3)

静态重载方法

  Public static string valueof()   可以将基本语句装换成 字符串,例如

Public static string valueof(double d)

Public  statyic string valueof(double d)

Public   static stringof(int i)

 

方法public string(string regex)可以将一个字符串按照指定的分割符分割 ,返回分隔后的字符串数组。

 

String类举例(4)

package test;

 

public class teststringclass04 {

  public static  void main(String[] args){

  int j=1234567;

  String sNumber=String.valueOf(j);

  System.out.println

  ("j是"+sNumber.length()+"位数。");

  String s="Mary,F,1976";

  String[] sPlit=s.split(",");

  for(int i=0;i<sPlit.length;i++){

   System.out.println(sPlit[i]);

  }

  }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值