String方法总结

charAt(int index)
返回 char指定索引处的值。

String s = “www.runoob.com”;
for (int i =0 ;i<s.length();i++){
char result = s.charAt(i);
System.out.print(result+" ");
}
返回值:w w w . r u n o o b . c o m
2.
2.1 codePointAt(int index)
返回指定索引处的字符(Unicode代码点)。
2.2 codePointBefore(int index)
返回指定索引之前的字符(Unicode代码点)。
2.3 codePointCount(int beginIndex, int endIndex)
返回此 String指定文本范围内的Unicode代码点数。
String s = “www.runoob.com”;
int result = s.codePointAt(4);
System.out.println(result); #114
int result1 = s.codePointBefore(5);
System.out.println(result1); #114
int result2 = s.codePointCount(0,3);
System.out.println(result2); #3
返回值:
2.1 114
2.2 114
2.3 3
3.
3.1 compareTo(String anotherString)
按字典顺序比较两个字符串。
3.2 compareToIgnoreCase(String str)
按字典顺序比较两个字符串,忽略大小写差异。
String s = “www.runoob.com”;
int value = s.compareTo(“www.runoob.com1”);
System.out.println(value); #1
int value1 = s.compareToIgnoreCase(“www.RUNoob.com”);
System.out.println(value1); #0
返回值:
3.1 1
3.2 0

concat(String str)
将指定的字符串连接到该字符串的末尾。
String s = “www.runoob.com”;
String str = s.concat(" I love you “);
System.out.println(str);
返回值:
www.runoob.com I love you
5.
contains(CharSequence s)
当且仅当此字符串包含指定的char值序列时才返回true。
String s = “www.runoob.com”;
Boolean str = s.contains(” I love you ");
System.out.println(str); #false
Boolean str1 = s.contains(“c”);
System.out.println(str1); #true
返回值:
false
true
6.
contentEquals(StringBuffer sb)
将此字符串与指定的StringBuffer进行 StringBuffer 。
public static void main(String args[]) {
String str1 = “String1”;
String str2 = “String2”;
StringBuffer str3 = new StringBuffer( “String1”);

    boolean  result = str1.contentEquals( str3 );
    System.out.println(result);  //true
      
    result = str2.contentEquals( str3 );
    System.out.println(result) ; //false
}
返回值:
true
false

contentEquals(CharSequence cs)
将此字符串与指定的CharSequence进行 CharSequence 。
String str1 = “amrood admin”,
str2 = “345”;
CharSequence cs = “amrood admin”;

	System.out.println("Method returns: " + str1.contentEquals("amrood admin")); //true
	
	System.out.println("Method returns: " + str1.contentEquals(cs)); //true
	
	System.out.println("Method returns: " + str2.contentEquals("12345")); //false
	返回值:
	true
	true
	false

7.1
String 和 CharSequence 关系
String 继承于CharSequence,也就是说String也是CharSequence类型。
CharSequence是一个接口,它只包括length(), charAt(int index), subSequence(int start, int end)这几个API接口。除了String实现了CharSequence之外,StringBuffer和StringBuilder也实现了CharSequence接口。
需要说明的是,CharSequence就是字符序列,String, StringBuilder和StringBuffer本质上都是通过字符数组实现的!
8.
public static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。

public static String copyValueOf(char[] data, int offset, int count): 返回指定数组中表示该字符序列的 字符串。
public static void main(String args[]) {
char[] Str1 = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ’ ', ‘r’, ‘u’, ‘n’, ‘o’, ‘o’, ‘b’};
String Str2 = “”;

    Str2 = Str2.copyValueOf( Str1 );
    System.out.println("返回结果:" + Str2);

    Str2 = Str2.copyValueOf( Str1, 2, 6 );
    System.out.println("返回结果:" + Str2);
}

返回值:
返回结果:hello runoob
返回结果:llo ru
9.
boolean equals(Object anObject)
将此字符串与指定对象进行比较。
public class Test {
public static void main(String args[]) {
String Str1 = new String(“runoob”);
String Str2 = Str1;
String Str3 = new String(“runoob”);
boolean retVal;

            retVal = Str1.equals( Str2 );
            System.out.println("返回值 = " + retVal );//true

            retVal = Str1.equals( Str3 );
            System.out.println("返回值 = " + retVal );//true
    }

}
String 重写equals方法比较的是值所以都相等

boolean equalsIgnoreCase(String anotherString)
将此 String与其他 String比较,不考虑大小写。。
11.
static String format(Locale l, String format, Object… args)
使用指定的区域设置,格式字符串和参数返回格式化的字符串。
12.
static String format(String format, Object… args)
使用指定的格式字符串和参数返回格式化的字符串。
13.
byte[] getBytes()
使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
14.
byte[] getBytes(Charset charset)
使用给定的charset将该String编码为字节序列,将结果存储到新的字节数组中。

byte[] getBytes(String charsetName)
使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
mport java.io.*;

public class Test {
public static void main(String args[]) {
String Str1 = new String(“runoob”);

    try{
        byte[] Str2 = Str1.getBytes();
        System.out.println("返回值:" + Str2 );
        
        Str2 = Str1.getBytes( "UTF-8" );
        System.out.println("返回值:" + Str2 );
        
        Str2 = Str1.getBytes( "ISO-8859-1" );
        System.out.println("返回值:" + Str2 );
    } catch ( UnsupportedEncodingException e){
        System.out.println("不支持的字符集");
    }
}

}

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将此字符串中的字符复制到目标字符数组中。
public class Test {
public static void main(String args[]) {
String Str1 = new String(“www.runoob.com”);
char[] Str2 = new char[6];

    try {
        Str1.getChars(4, 10, Str2, 0);
        System.out.print("拷贝的字符串为:" );
        System.out.println(Str2 );//runoob
    } catch( Exception ex) {
        System.out.println("触发异常...");
    }
}

}

int hashCode()
返回此字符串的哈希码。

int indexOf(int ch) ch – 字符,Unicode 编码。
返回指定字符第一次出现的字符串内的索引。

int indexOf(int ch, int fromIndex) fromIndex – 开始搜索的索引位置,第一个字符是 0 ,第二个是 1 ,以此类推。
返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。

int indexOf(String str)
返回指定子字符串第一次出现的字符串内的索引。
String string = “aaa456ac”;
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf(“b”)); // indexOf(String str); 返回结果:-1,"b"不存在

int indexOf(String str, int fromIndex)
返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf(“a”,3));//indexOf(String str, int fromIndex); 返回结果:6

String intern()
返回字符串对象的规范表示。

boolean isEmpty()
返回 true如果,且仅当 length()为 0 。

static String join(CharSequence delimiter, CharSequence… elements)
返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter 。

static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
返回一个新 String的副本组成 CharSequence elements与指定的副本一起加入 delimiter 。

int lastIndexOf(int ch)
返回指定字符的最后一次出现的字符串中的索引。

int lastIndexOf(int ch, int fromIndex)
返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
int lastIndexOf(String str)
返回指定子字符串最后一次出现的字符串中的索引。

int lastIndexOf(String str, int fromIndex)
返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。

int length()
返回此字符串的长度。

boolean matches(String regex)
告诉这个字符串是否匹配给定的 regular expression 。
public class Test {
public static void main(String args[]) {
String Str = new String(“www.runoob.com”);

    System.out.print("返回值 :" );
    System.out.println(Str.matches("(.*)runoob(.*)"));//true
    
    System.out.print("返回值 :" );
    System.out.println(Str.matches("(.*)google(.*)"));//false

    System.out.print("返回值 :" );
    System.out.println(Str.matches("www(.*)"));//true
}

}

返回此 String内的指数,与 index codePointOffset代码点。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
ignoreCase – 如果为 true,则比较字符时忽略大小写。

toffset – 此字符串中子区域的起始偏移量。

other – 字符串参数。

ooffset – 字符串参数中子区域的起始偏移量。

len – 要比较的字符数。
public static void main(String args[]) {
String Str1 = new String(“www.runoob.com”);
String Str2 = new String(“runoob”);
String Str3 = new String(“RUNOOB”);

    System.out.print("返回值 :" );
    System.out.println(Str1.regionMatches(4, Str2, 0, 5));//true

    System.out.print("返回值 :" );
    System.out.println(Str1.regionMatches(4, Str3, 0, 5));//fasle

    System.out.print("返回值 :" );
    System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));//true
}

String replace(char oldChar, char newChar)
返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
String replace(CharSequence target, CharSequence replacement)
将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
String replaceAll(String regex, String replacement)
用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串。
String replaceFirst(String regex, String replacement)
用给定的替换替换与给定的 regular expression匹配的此字符串的第一个子字符串。

String[] split(String regex)
将此字符串分割为给定的 regular expression的匹配。
String[] split(String regex, int limit)
将这个字符串拆分为给定的 regular expression的匹配。
注意: . 、 $、 | 和 * 等转义字符,必须得加 \。

注意:多个分隔符,可以用 | 作为连字符。

regex – 正则表达式分隔符。

limit – 分割的份数。

public static void main(String args[]) {
String str = new String(“Welcome-to-Runoob”);

    System.out.println("- 分隔符返回值 :" );
    for (String retval: str.split("-")){
        System.out.println(retval);
    }
  • 分隔符返回值 :
    Welcome
    to
    Runoob

      System.out.println("");
      System.out.println("- 分隔符设置分割份数返回值 :" );
      for (String retval: str.split("-", 2)){
          System.out.println(retval);
      }
    
  • 分隔符设置分割份数返回值 :
    Welcome
    to-Runoob

     System.out.println("");
     String str2 = new String("www.runoob.com");
     System.out.println("转义字符返回值 :" );
     for (String retval: str2.split("\\.", 3)){
         System.out.println(retval);
     }
    

转义字符返回值 :
www
runoob
com

    System.out.println("");
    String str3 = new String("acount=? and uu =? or n=?");
    System.out.println("多个分隔符返回值 :" );
    for (String retval: str3.split("and|or")){
        System.out.println(retval);
    }
}

多个分隔符返回值 :
acount=?
uu =?
n=?

boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开头。
boolean startsWith(String prefix, int toffset)
测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
CharSequence subSequence(int beginIndex, int endIndex)
返回一个字符序列,该序列是该序列的子序列。
String substring(int beginIndex)
返回一个字符串,该字符串是此字符串的子字符串。
String substring(int beginIndex, int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。

char[] toCharArray()
将此字符串转换为新的字符数组。
String toLowerCase()
将所有在此字符 String使用默认语言环境的规则,以小写。
String toLowerCase(Locale locale)
将所有在此字符 String ,以降低使用给定的规则情况下 Locale 。
String toString()
此对象(已经是字符串!)本身已被返回。
String toUpperCase()
将所有在此字符 String使用默认语言环境的规则大写。
String toUpperCase(Locale locale)
将所有在此字符 String使用给定的规则,大写 Locale 。
String trim()
返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。

valueOf(删除头尾空白符的字符串)
static String valueOf(boolean b)
返回 boolean参数的字符串 boolean形式。
static String valueOf(char c)
返回 char参数的字符串 char形式。
static String valueOf(char[] data)
返回 char数组参数的字符串 char形式。
static String valueOf(char[] data, int offset, int count)
返回 char数组参数的特定子阵列的字符串 char形式。
static String valueOf(double d)
返回 double参数的字符串 double形式。
static String valueOf(float f)
返回 float参数的字符串 float形式。
static String valueOf(int i)
返回 int参数的字符串 int形式。
static String valueOf(long l)
返回 long参数的字符串 long形式。
static String valueOf(Object obj)
返回 Object参数的字符串 Object形式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值