Java笔记(三)String的构造方法、常用方法和拼接

1 关于String类中的构造方法

String s = new String(" ");
String s = " ";
String s = new String(char数组);
String s = new String(char数组,起始下标,长度);
String s = new String(byte数组);
String s = new String(bety数组,起始下标,长度);

2 关于String类的方法
2.1 charAt(int index);

char c = "中国人".charAt(1); //c 为“国”;

2.2 int copareTo(String anotherString)

int result = "abc".compareTo("abc"); //0
int result1 = "abcd".compareTo("abce"); //-1
int result2 = "abce".compareTo(“abcd”); //1

2.3 boolean contains(CharSequence s)
判断前面的字符串是否包含后面的字符串

System.out.println("123456".contains("123")); //true
System.out.println("123456".contains("567")); //false

2.4 boolean endWith(String suffix)
判断当前字符串是否以某个字符串结尾。

System.out.println("test.txt".endsWith("txt")); //true
 System.out.println("test.txt".endsWith("doc")); //false

2.5 boolean equals(Object anObject)
比较两个字符串是否相同

System.out.println("abc".equals("abc")); //true

2.6 boolean equalsIgnoreCase(String anotherString)
判断两个字符串是否相等,并且忽略大小写

System.out.println("abcDEF".equalsIgnoreCase("abcdef")); //true

2.7 byte[ ] getBytes();
将字符串转成字节数组

byte[] bytes = "abcdef".getBytes();
        for(int i = 0; i < bytes.length; i++){
            System.out.println(bytes[i]);
        }

2.8 int indexOf
判断某个字符串在当前字符串中第一次出现处的索引(下标)

System.out.println("oraclejavac++.netjava".indexOf("java")); //6

2.9 bollean isEmpty()
判断某个字符串是否为空

String s = "";
    System.out.println(s.isEmpty()); //true

2.10 int length()
判断字符串长度

System.out.println("abc".length()); //3

2.11 int LastIndexOf(String str)
判断某个字符串在当前字符串中最后一次出现的索引(下标)

System.out.println("abcjavajggihhjavajihdjava".lastIndexOf("java"));

2.12 String replace(CharSequence, CharSequence replacement)

  String s = "http://www.baidu.com".replace(target:"http://”,replacement:“https://);
     System.out.println("abc");    //https://www.baidu.com

2.13 String[] split(String regex)
拆分字符串

  String[] ymd = "1980-10-11".split("-");
        for (int i = 0; i <= ymd.length; i++){
            System.out.println(ymd[i]);
        }

2.14 boolean startsWith(String prefix)
判断某个字符串是否以某个字符串开始

System.out.println("test.txt".endsWith("testt")); //true
System.out.println("test.txt".endsWith("abc")); //false

2.15 String substring(int beginIndex)
截取beginIndex以后的字符串

 System.out.println("abcdefghijk".substring(2));  //cdefghijk

2.16 String substring(int beginIndex, int endIndex)
截取beginIndex以后,endIndex以前的字符串
beginIndex起始位置(包括)
endIndex结束位置(不包括)

 System.out.println("abcdefghijk".substring(2,5));  //cde

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

 char[] chars = "我是中国人“.toCharArray();

2.18 String toLowerCase()

System.out.println("ABCdeFG".toLowerCase());

转成小写
2.19 String toUpperCase()
转换为大写

 System.out.println("abcdefg".toUpperCase());

2.20 String trim()
去除字符串前后空白

 System.out.println("     hello        word     ! ".trim());

2.21 String valueOf()
String中只有一个是静态的,不需要new对象
把非字符串转换为字符串

String s1 = String.valueOf(100);
   String s2 = String.valueOf(3.14);
   String s3 = String.valueOf(true);

3.1 StringBuffer
创建的字符串可以拼接(多线程环境下是安全的)

StringBuffer stringBuffer = new StringBuffer();

        //append是追加的意思
        stringBuffer.append("a");
        stringBuffer.append("b");
        stringBuffer.append(3.14);
        System.out.println(stringBuffer);

3.2 StringBuilder
创建的字符串可以拼接(多线程环境下是不安全的)

public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        //append是追加的意思
        sb.append("a");
        sb.append("b");
        sb.append(3.14);
        System.out.println(sb);
    }

限于作者水平有限,有不足之处望大家指正!!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值