A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

1,多个字符组成的一个序列,叫字符串。
生中多数据的描述都采用的是字符串的。而且我们还会对其进行操作。
所以,java就提供了这样的一个类供我们使用。
2,创建字符串对象
[url=]                               [/url]
    A:String():无参构造
    举例:
    String s = new String();
    s = "hello";
    sop(s);
   B:String(byte[] bys):传一个字节数组作为参数 *****
    举例
   byte[] bys = {97,98,99,100,101};
   String s = new String(bys);
   sop(s);
   C:String(byte[] bys,int index,int length):把字节数组的一部分转换成一个字符串 *****
    举例
    byte[] bys = {97,98,99,100,101};
    String s = new String(bys,1,2);
    sop(s);
   D:String(char[] chs):传一个字符数组作为参数 *****
    举例
   char[] chs = {'a','b','c','d','e'};
   String s = new String(chs);
   sop(s);
   E:String(char[] chs,int index,int length):把字符数组的一部分转换成一个字符串 *****
    举例
   char[] chs = {'a','b','c','d','e'};
   String s = new String(chs,1,2);
   sop(s);   
   F:String(String str):把一个字符串传递过来作为参数
   char[] chs = {'a','b','c','d','e'};
   String ss = new String(s);
   sop(ss);
   G:直接把字符串常量赋值给字符串引用对象(最常用) *****
    举例
   String s = "hello";
   sop(s);


3,面试题
   A:请问String s = newString("hello");创建了几个对象。
   两个。一个"hello"字符串对象,在方法区的常量池;一个s对象,在栈内存。
   B :请写出下面的结果
   String s1 = new String("abc");
   Strign s2 = new String("abc");
   String s3 = "abc";
   String s4 = "abc";
   sop(s1==s2); //false
   sop(s1==s3); //false
   sop(s3==s4); //true
   C:字符串对象一旦被创建就不能被改变。
   指的是字符串常量值不改变。
4,字符串中各种功能的方法
   A:判断
   **** boolean equals(Object anObject):判断两个字符串的内容是否相同,复写了Object的方法
   **** boolean equalsIgnoreCase(String anotherString):判断两个字符串的内容是否相同,
   不区分大小写
   **** boolean contains(String s):判断一个字符串中是否包含另一个字符串
   注意:判断字符串是否包含特殊字符.直接表示为str.contains(".")
   boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
   boolean startsWith(String suffix):测试此字符串是否以指定的前缀开始
   boolean isEmpty():测试字符串是否为空
   B :获取
   ***** int length():返回此字符串的长度
   ***** char charAt(int index):返回指定索引处的 char值
   ***** int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
   int indexOf(int ch, int fromIndex):返回在此字符串中第一次出现指定字符处的索引,
   从指定的索引开始搜索。
   int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。   
   int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次
   出现处的索引,从指定的索引开始。
   *** int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。
   int lastIndexOf(int ch, int fromIndex)
   返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
   int lastIndexOf(String str)
   返回指定子字符串在此字符串中最右边出现处的索引。
   int lastIndexOf(String str, int fromIndex)
   返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
   ***** String substring(int beginIndex) (注意:该方法substring的String是小写!!!)   
   返回一个新的字符串,它是此字符串的一个子字符串。
   String substring(int beginIndex, int endIndex) (注意该方法的String是小写!!!)
   返回一个新字符串,它是此字符串的一个子字符串,包含头不包含尾。   
   转换 :***** byte[] getBytes():(很常用!)从字符串到字节数组的方法
   void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)   
   将字符从此字符串复制到目标字符数组。
   ***** char[] toCharArray():(很常用!)从字符串到字符数组的方法
   **** static String copyValueOf(char[] data)
   返回指定数组中表示该字符序列的 String。
   static String copyValueOf(char[] data, int offset, int count)
   返回指定数组中表示该字符序列的 String。
   ***** static String valueOf(数据类型):把该数据类型的数据转换成字符串。
   *** String toLowerCase():把字符串转换成小写
   String toUpperCase():把字符串转换成大写
   字符串的连接 : String concat(String str):将指定字符串连接到此字符串的结尾。
   替换 :String replace(char oldChar, char newChar):用新字符替换旧字符(替换所有)
           String replace(String target, String replacement):用新的子串换旧串
   分割 : String[] split(String regex):根据指定的字符串把一个字符串分割成一个字符串数组


   String trim():去除字符串的前后空格

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




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马