方法 解释说明
char charAt(int index)
返回字符串中第 index 个字符
boolean equals(String other)
如果字符串与 other 相等,返回 true;否则,返回 false。
boolean equalsIgnoreCase(String other)
如果字符串与 other 相等(忽略大小写),则返回 true;否则,返回 false。
int indexOf(String str)
返回从头开始查找第一个子字符串 str 在字符串中的索引位置。如果未找到子字符串 str,则返回-1。
lastIndexOf()
返回从末尾开始查找第一个子字符串 str 在字符串中的索引位置。如果未找到子字符串 str,则返回-1。
int length()
返回字符串的长度。
String replace(char oldChar,char newChar)
返回一个新串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。
boolean startsWith(String prefix)
如果字符串以 prefix 开始,则返回 true。
boolean endsWith(String prefix)
如果字符串以 prefix 结尾,则返回 true。
String substring(int beginIndex)
返回一个新字符串,该串包含从原始字符串 beginIndex 到串尾。
String substring(int beginIndex,int endIndex)
返回一个新字符串,该串包含从原始字符串 beginIndex 到串尾或 endIndex-1 的所有字符。
String toLowerCase()
返回一个新字符串,该串将原始字符串中的所有大写字母改成小写字母。
String toUpperCase()
返回一个新字符串,该串将原始字符串中的所有小写字母改成大写字母。
String trim()
返回一个新字符串,该串删除了原始字符串头部和尾部的空格

代码测试:

package demo4;

public class StringTest {
    public static void main(String[] args) {
        String s="abcd";
        String s1=new String("abcd");
        System.out.println(s==s1);  //  地址相比较结果false
        System.out.println(s.equals(s1));//值比较结果true
        System.out.println(s.substring(1));//从下标为1的地方截取
        System.out.println(s.substring(1,3));//从下标为1截取,到下标为3结束不包含3
        String value="abcdefjf";
        System.out.println(value.charAt(3));//索引为3的是d
        System.out.println(value.replace("fj","&"));//把fj替换成&
        System.out.println(value.indexOf("e"));//字符串e的索引位置4
        System.out.println(value.indexOf("BB"));//是否包含这个字符串,不包含返回-1
        System.out.println(value.lastIndexOf("f"));//字符串f最后出现的位置7
        System.out.println(value.startsWith("a"));//是否以a开头true
        System.out.println(value.endsWith("f"));//是否以f结尾
        String value1=" I love you !";
        System.out.println(value1.trim());//去掉所有的空格
        System.out.println(value1.replace(" ",""));//把所有空格都去掉
        System.out.println(value1.toUpperCase());//字符串转为大写字母
        System.out.println(value1.toLowerCase());//字符串转为小写字母
    }
}

 

StringBuffer 和 StringBuilder

 

  • 常用方法列表:

 

  1.  重载的 public StringBuilder append(…)方法可以为该 StringBuilder 对象添加字符序列,仍然返回自身对象。

  2. 方法 public StringBuilder delete(int start,int end)可以删除从 start 开始到 end-1 为止的一段字符序列,仍然返回自身对象。

  3. 方法 public StringBuilder deleteCharAt(int index)移除此序列指定位置上的 char,仍然返回自身对象。

  4. 重载的 public StringBuilder insert(…)方法可以为该 StringBuilder 对象在指定位置插入字符序列,仍然返回自身对象。

  5. 方法 public StringBuilder reverse()用于将字符序列逆序,仍然返回自身对象。

  6. 方法 public String toString() 返回此序列中数据的字符串表示形式。

  7. 和 String 类含义类似的方法:

 

public int indexOf(String str)

 

public int indexOf(String str,int fromIndex)

 

public String substring(int start)

 

public String substring(int start,int end)

 

public int length()

 

char charAt(int index)

 

  • String:不可变字符序列。

  • StringBuffer:可变字符序列,并且线程安全,但是效率低。

  • StringBuilder:可变字符序列,线程不安全,但是效率高(一般用它)。

 

package com.sxt.demo;

public class TestString {
    public static void main(String[] args) {
        String s="abCde ";
        System.out.println(s.charAt(3));//索引为3的是d
        System.out.println(s.toLowerCase());//将这个字符串改为小写
        System.out.println(s.toUpperCase());//将这个字符串改成大写
        System.out.println(s.substring(0,3));//截取0-3索引的字符串
        System.out.println(s.replace("d","o"));//替换
        System.out.println(s.trim());//去掉空格
        System.out.println(s.lastIndexOf("e"));//最后一个e出现的索引位置
        System.out.println(s.indexOf(3));//字符串是否包含3,不包含返回-1
        System.out.println(s.endsWith(" "));//是否以空格结尾
        System.out.println(s.startsWith("a"));//是否以a开头
        StringBuilder value=new StringBuilder();
        value.append("我是");
        value.append("一个");
        value.append("可爱的人");//追加字符的意思
        System.out.println(value);
        for(int i=1;i<=10;i++){
            value.append(i);
        }
        System.out.println(value);
        StringBuffer a=new StringBuffer("永远开心快乐");
        System.out.println(a.delete(4,6));//索引为4-6的地方删除
        System.out.println(a.insert(0,"我"));//在索引为0的地方插入我
        System.out.println(a.insert(1,"会"));//在索引为1的地方插入会
        System.out.println(a.reverse());//字符串逆向
    }
}

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/123sougou/p/12680252.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!