Java--常用的String方法


contains() 方法

public boolean contains(CharSequence s)

当且仅当此字符串包含指定的 char 值序列时,返回 true。即一个A字符串中包涵B字符串,且B字符串在A字符串中的字符顺序相同。

实例
 String  A = "abcdefghijklmnopqrstuvwxyz";
   String  B ="abc";
   String  C ="cba";
 system.out.println("A-->B  "+A.contains(B));
 system.out.println("A-->C  "+A.contains(C));

结果为:

  A-->B  true 
  A-->C  false

split() 方法

public String[] split(String regex, int limit)
split() 方法根据匹配给定的正则表达式来拆分字符串。

实例:
String  a="hello World 12313";
        String s1 = a.split(" ")[0];
        String s2 = a.split(" ")[1];
        String s3=a.split(" ")[2];
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

结果为:

hello
World
123

注意:

  • . 、 | 和 * 等转义字符,必须得加 \。

  • 多个分隔符,可以用 | 作为连字符

substring() 方法

public String substring(int beginIndex)

public String substring(int beginIndex, int endIndex)
substring() 方法返回字符串的子字符串。即通过字符串下标截取一段字符串。

实例
String a="abcdefghijklmnopqrstuvwxyz";
 System.out.println("substring()-->  "+a.substring(1));
 System.out.println("substring()-->  "+a.substring(1, 14));

结果为:

substring()-->  bcdefghijklmnopqrstuvwxyz
substring()-->  bcdefghijklmn

replace() 方法

public String replace(char oldChar,char newChar)

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

实例:
String a="abcdefghijklmnopqrstuvwxyz";
System.out.println(" replace()-->  "+a.replace("abc", "cba"));

结果为:

 replace()-->  cbadefghijklmnopqrstuvwxyz

toUpperCase()方法

public String toUpperCase()

public String toUpperCase(Locale locale)

将字符串小写字符转换为大写。

实例:
 String a="abcdefghijklmnopqrstuvwxyz";
 System.out.println("toUpperCase()-->  "+s);

结果为:

toUpperCase()-->  ABCDEFGHIJKLMNOPQRSTUVWXYZ

toLowerCase() 方法

**public String toLowerCase()

public String toLowerCase(Locale locale)**
将字符串大写字符转换为小写。

实例:
 String a="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 System.out.println("toLowerCase()-->  "+s);

结果为:

   toLowerCase()-->  abcdefghijklmnopqrstuvwxyz

to CharArray()方法

public char[] toCharArray()
将字符串转换为字符数组。

实例:
String a="abcdefghijklmnopqrstuvwxyz";
char[] chars = a.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            System.out.print(" ");
        }

结果为:

a b c d e f g h i j k l m n o p q r s t u v w x y z

startsWith() 方法

public boolean startsWith(String prefix, int toffset)

public boolean startsWith(String prefix)

用于检测字符串是否以指定的前缀开始。

参数
  • prefix – 前缀。

  • toffset – 字符串中开始查找的位置。

实例
public class Test {
    public static void main(String args[]) {
        String Str = new String("www.baidu.com");
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("www") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("baidu") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("baidu", 4) );
    }
}

结果为:

返回值 :true
返回值 :false
返回值 :true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值