String一些方法的使用

  • char charAt(int index)
    返回指定索引处的char值
 	String temp0 = "helloworld";
 	System.out.println(temp0.charAt(7));

  • int compareTo(Object o)
    把这个字符串和另一个对象比较
    int compareTo(String anotherString)
    按字典顺序比较两个字符串

    如果参数字符串等于此字符串,则返回值 0;
    如果此字符串小于字符串参数,则返回一个小于 0 的值;
    如果此字符串大于字符串参数,则返回一个大于 0 的值
    如果第一个字符和参数的第一个字符不等,结束比较,返回第一个字符的ASCII码差值。
    如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至不等为止,返回该字符的ASCII码差值。
    如果两个字符串不一样长,可对应字符又完全一样,则返回两个字符串的长度差值。

	String str1 = "STrings";
	String str2 = "Strings";
	String str3 = "Strings123";
	System.out.println(str1.compareTo(str2));
	System.out.println(str2.compareTo(str3));
	System.out.println(str3.compareTo(str1));

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

    如果参数字符串等于此字符串,则返回值 0;
    如果此字符串小于字符串参数,则返回一个小于 0 的值;
    如果此字符串大于字符串参数,则返回一个大于 0 的值。

	String str1 = "FJTY";
    String str2 = "Fjty";
    String str3 = "Fjty123";
    System.out.println(str1.compareToIgnoreCase(str2));
    System.out.println(str2.compareToIgnoreCase(str3));
    System.out.println(str3.compareToIgnoreCase(str1));

  • String concat(String str)
    将指定字符串连接到此字符串的结尾
 	String temp0 = "llw";
    System.out.println(temp0.concat("txdy"));

  • boolean contentEquals(StringBuffer sb)
    如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false
	String str1 = "baidu1";
    String str2 = "baidu2";
    StringBuffer str3 = new StringBuffer("baidu1");
    System.out.println(str1.contentEquals(str3));
    System.out.println(str2.contentEquals(str3));

  • static String copyValueOf(char[] data)
    返回指定数组中表示该字符序列的 String
    static String copyValueOf(char[] data, int offset, int count)
    返回指定数组中表示该字符序列的 String
 	char[] chars = {'h', 'e', 'l', 'l', 'o', ' ', 'l', 'l', 'w', 'd', 'y'};
    String temp0 = "";
    System.out.println(temp0.copyValueOf(chars));
    System.out.println(temp0.copyValueOf(chars, 2, 6));

  • boolean endsWith(String suffix)
    测试此字符串是否以指定的后缀结束
	String temp0 = "llwtxdy";
    System.out.println(temp0.endsWith("dy"));

  • boolean equals(Object anObject)
    将此字符串与指定的对象比较
  	String temp0 = "FjTy";
    String temp1 = new String("FjTy");
    String temp2 = temp0;
    System.out.println(temp0.equals(temp1));
    System.out.println(temp0.equals(temp2));

  • boolean equalsIgnoreCase(String anotherString)
    将此 String 与另一个 String 比较,比较字符串内容,不考虑大小写
  	String temp0 = "FjTy";
    System.out.println(temp0.equalsIgnoreCase("fjty"));
    System.out.println(temp0.equalsIgnoreCase(new String("fjty")));

  • byte[] getBytes()
    使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
    byte[] getBytes(String charsetName)
    使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
    charsetName支持的字符集名称,有ISO-8859-1、UTF-8等值
	try {
        String temp0 = "FjTy";
        System.out.println(temp0.getBytes());
        System.out.println(temp0.getBytes("UTF-8"));
        System.out.println(temp0.getBytes("ISO-8859-1"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

  • void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
    将字符从此字符串复制到目标字符数组
    srcEnd要写到最后的字符之后的索引
    必须满足dst.length-dstBegin>=srcEnd-srcBegin,否则抛出IndexOutOfBoundsException异常
 	String temp0 = "FjlvTy";
    char[] chars = new char[6];
    temp0.getChars(2,6,chars,1);
    System.out.println(chars);

  • int hashCode()
    返回此字符串的哈希码
 	String temp0 = "FjTy";
    System.out.println(temp0.hashCode());

  • int indexOf(int ch)
    返回指定字符在此字符串中第一次出现处的索引
    int indexOf(int ch, int fromIndex)
    返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
    int indexOf(String str)
    返回指定子字符串在此字符串中第一次出现处的索引
    int indexOf(String str, int fromIndex)
    返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
  	String temp0 = "sibal-allow-app";
    String temp1 = "www.google.com";
    String temp2 = "llwtxdy,czqtxdy";
    String temp3 = "llwtxdy,czqtxdy";
    System.out.println(temp0.indexOf(97));//97(a)
    System.out.println(temp1.indexOf(111,6));//111(o)
    System.out.println(temp2.indexOf("txdy"));
    System.out.println(temp3.indexOf("txdy",5));

  • String intern()
    返回字符串对象的规范化表示形式
    去这看看吧
 	String a = "fjty";
    String b = "fjty";
    System.out.println(a == b);

    String aa = new String("fjty");
    String bb = new String("fjty");
    System.out.println(aa == bb);

    String c = "c";
    String cc = new String("c");
    System.out.println(c == cc);
    String d = "d";
    String dd = new String("d").intern();
    System.out.println(d == dd);

  • int lastIndexOf(int ch)
    返回指定字符在此字符串中最后一次出现处的索引
    int lastIndexOf(int ch, int fromIndex)
    返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索
    int lastIndexOf(String str)
    返回指定子字符串在此字符串中最右边出现处的索引
    int lastIndexOf(String str, int fromIndex)
    返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
    ch为字符的ASCII码值,a(97),A(65)
    找不到就返回-1
    当有索引fromIndex时,才反向搜索(从右往左),但是查找的是从右往左第一个出现的位置了,也是从左往右的最后一个。
  	String temp0 = "llwtxdy,czqtxdy";
    String temp1 = "www.google.com";
    String temp2 = "llwtxdy,czqtxdy";
    String temp3 = "www.google.com";
    System.out.println(temp0.lastIndexOf(108));//108(l)
    System.out.println(temp1.lastIndexOf(111,11));//111(o)
    System.out.println(temp2.lastIndexOf("czq"));
    System.out.println(temp3.lastIndexOf("oo",11));

  • int length()
    返回此字符串的长度
	String temp0 = "llwtxdy";
    System.out.println(temp0.length());

  • boolean matches(String regex)
    告知此字符串是否匹配给定的正则表达式
	String temp0 = "www.baidu.com";
    System.out.println(temp0.matches("(.*)baidu(.*)"));
    System.out.println(temp0.matches("(.*)google(.*)"));
    System.out.println(temp0.matches("www(.*)"));

  • 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要比较的字符数
	String temp0 = "www.baidu.com";
    String temp1 = "baidu";
    String temp2 = "BAIDU";
    System.out.println(temp0.regionMatches(4, temp1, 0, 5));
    System.out.println(temp0.regionMatches(4, temp2, 0, 5));
    System.out.println(temp0.regionMatches(true, 4, temp2, 0, 5));

  • String replace(char oldChar, char newChar)
    返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
	String temp0 = "llwtxdy";
    System.out.println(temp0.replace('l','z'));

  • String replaceAll(String regex, String replacement)
    使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串
	String temp0 = "www.google.com";
    System.out.print("匹配成功返回值 :" );
    System.out.println(temp0.replaceAll("(.*)google(.*)", "baidu" ));
    System.out.print("匹配失败返回值 :" );
    System.out.println(temp0.replaceAll("(.*)taobao(.*)", "baidu" ));

  • String replaceFirst(String regex, String replacement)
    使用给定的 replacement 替换此字符串匹配给定的正则表达式regex的第一个子字符串
	String temp0 = "hello runoob,I am from runoob。";
    String temp1 = "hello runoob,I am from runoob。";
    System.out.println(temp0.replaceFirst("runoob","baidu"));
    System.out.println(temp1.replaceFirst("(.*)runoob(.*)","baidu"));

  • String[] split(String regex)
    根据给定正则表达式的匹配拆分此字符串
    String[] split(String regex, int limit)
    根据匹配给定的正则表达式来拆分此字符串
    . $ | 和 * 等转义字符,必须得加 \
    当分隔符有多个时,用 | 作为连接
	String temp0 = "Welcome-to-Runoob";
    for (String str: temp0.split("-")){
        System.out.println(str);
    }
    System.out.println();
    String temp1 = "Welcome-to-Runoob";
    for (String str: temp1.split("-", 2)){
        System.out.println(str);
    }
    System.out.println();
    String temp2 = "www.runoob.com";
    for (String str: temp2.split("\\.", 3)){
        System.out.println(str);
    }
    System.out.println();
    String temp3 = "acount=? and uu =? or n=?";
    for (String str: temp3.split("and|or")){
        System.out.println(str);
    }

  • boolean startsWith(String prefix)
    测试此字符串是否以指定的前缀开始
	String temp0 = "llwtxdy";
    System.out.println(temp0.startsWith("llw"));

  • boolean startsWith(String prefix, int toffset)
    测试此字符串从指定索引toffset开始的子字符串是否以指定前缀开始
 	String temp0 = "llwtxdy";
    System.out.println(temp0.startsWith("wt",2));

  • String substring(int beginIndex)
    返回一个新的字符串,它是此字符串的一个子字符串
 	String temp0 = "czqtxdy";
    System.out.println(temp0.substring(1));

  • String substring(int beginIndex, int endIndex)
    返回一个新字符串,它是此字符串的一个子字符串
    beginIndex起始索引(包括),索引从0开始;endIndex结束索引(不包括)
    在这里插入图片描述
	String temp0 = "czqtxdy";
    System.out.println(temp0.substring(2,4));

  • char[] toCharArray()
    将此字符串转换为一个新的字符数组
	String temp0 = "czqtxdy";
    char[] temp1 = temp0.toCharArray();
    for (char i:temp1) {
        System.out.print(i + " ");
    }

  • String toLowerCase()
    将字符串小写字符转换为大写
	String temp0 = "FJTY";
    System.out.println(temp0.toLowerCase());

  • String toUpperCase()
    将字符串小写字符转换为大写
	String temp0 = "FjTy";
    System.out.println(temp0.toUpperCase());

  • String trim()
    删除字符串的头尾空白符
	String temp0 = "   Fj Ty   ";
    String temp1 = temp0.trim();
    System.out.println("原字符串:" + temp0);
    System.out.println("原字符串长度:" + temp0.length());
    System.out.println("trim方法处理后:" + temp1);
    System.out.println("trim方法处理后长度:" + temp1.length());

  • static String valueOf(primitive data type x)
    返回给定data type类型x参数的字符串表示形式
    valueOf(boolean b)
    返回 boolean 参数的字符串表示形式。
    valueOf(char c)
    返回 char 参数的字符串表示形式。
    valueOf(char[] data)
    返回 char 数组参数的字符串表示形式。
    valueOf(char[] data, int offset, int count)
    返回 char 数组参数的特定子数组的字符串表示形式。
    valueOf(double d)
    返回 double 参数的字符串表示形式。
    valueOf(float f)
    返回 float 参数的字符串表示形式。
    valueOf(int i)
    返回 int 参数的字符串表示形式。
    valueOf(long l)
    返回 long 参数的字符串表示形式。
    valueOf(Object obj)
    返回 Object 参数的字符串表示形式。
 	boolean temp0 = true;
    char temp1 = 'T';
    char[] temp2 = {'F','j','T','y'};
    char[] temp3 = {'F','j','L','T','y'};
    double temp4 = 3.14;
    float temp5 = 4.13f;
    int temp6 = 85;
    long temp7 = 190422l;
    Object temp8 = "a,zhe";
    System.out.println(String.valueOf(temp0));
    System.out.println(String.valueOf(temp1));
    System.out.println(String.valueOf(temp2));
    System.out.println(String.valueOf(temp3,2,2));
    System.out.println(String.valueOf(temp4));
    System.out.println(String.valueOf(temp5));
    System.out.println(String.valueOf(temp6));
    System.out.println(String.valueOf(temp7));
    System.out.println(String.valueOf(temp8));

  • boolean contains(CharSequence chars)
    判断是否包含指定的字符系列
	System.out.println("Runoob".contains("Run"));   //true
    System.out.println("Runoob".contains("o"));     //true
    System.out.println("Runoob".contains("s"));     //false

  • boolean isEmpty()
    判断字符串是否为空
    字符串通过length()方法计算字符串长度,如果返回 0,即为空字符串
	System.out.println("Runoob".isEmpty()); //false
    System.out.println("".isEmpty());       //true
    System.out.println("      ".isEmpty()); //false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值