java学习之字符串

时间:2021-04-21 12:02:46   收藏:0   阅读:0

Java字符串

package chapter8;
public class chapt01 {
    public static void main(String[] args) {
        // 创建字符串对象
        String s1 = "";
        String s2 = new String("hello world");
        char[] chars = {‘a‘,‘b‘,‘c‘,‘d‘};
        String s3 = new String(chars);
        byte[] bytes = {97,98,99};
        String s4 = new String(bytes);
        String s5 = new String(chars,1,2);
            }
}
package chapter8;

import java.util.Date;

public class chapt02 {
    public static void main(String[] args) {
        String s1 = "hello ";
        s1 += "world";
        String s2 = "Hello";
        s2 = s2.concat(" ").concat("World");
        Date time = new Date();

        System.out.println(s1);
        System.out.println(s2);
        // 对象拼接自动采用toString()方法
        System.out.println("Today is " + time);
    }
}
package chapter8;

public class chapt03 {
    public static void main(String[] args) {
        String sourceStr = "this is a string accessing example";
        int firstChar = sourceStr.indexOf(‘r‘);
        int secondChar = sourceStr.indexOf(‘r‘,20);
        // 表达式的值从索引值为20开始往后面搜索
        int firstString = sourceStr.indexOf("is");
        int secondString = sourceStr.indexOf("is",5);
        int lastChar1 = sourceStr.lastIndexOf(‘r‘);
        int lastChar2 = sourceStr.lastIndexOf(‘r‘,5);
        int lastString1 = sourceStr.lastIndexOf("is");
        int lastString2 = sourceStr.lastIndexOf("is",2);
        int length = sourceStr.length();
    }
}
package chapter8;

public class chapt04 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("HELLO");
        // 比较两个字符串的内容是否相同 true
        System.out.println(s1.equals(s2));
        // 比较两个字符串是否为相同的引用 false
        System.out.println(s1 == s2);
        // 忽略大小写的情况 false true
        System.out.println(s1.equals(s3));
        System.out.println(s1.equalsIgnoreCase(s3));

        String s4 = new String("python");
        String s5 = new String("java");
        String s6 = new String("go");
        // 比较两个字符串的大小
        // s5 < s4 返回小于0的数字 , s5 > s6 返回大于零的数字
        System.out.println(s5.compareTo(s4));
        System.out.println(s5.compareTo(s6));
        // 忽略大小写的字符串比较 返回值为0
        System.out.println(s4.compareToIgnoreCase("PYTHON"));

        // 比较前缀和后缀
        String[] fileFolder =  {"java.doc","python.xls","hello.xls","go.doc","html.doc"};
        int wordCounter = 0;
        for(String file : fileFolder){
            file = file.trim();
            if(file.endsWith(".doc")) {
                wordCounter++;
            }
        }
        int xlsCounter = 0;
        for (String file : fileFolder){
            file = file.trim();
            if (file.endsWith(".xls")){
                xlsCounter++;
            }
        }
        System.out.println("文档数量是:" + wordCounter);
        System.out.println("表格数量是:" + xlsCounter);
    }
}
package chapter8;

public class chapt05 {
    public static void main(String[] args) {
        String sourceStr = new String("There is a string accessing example");
        String subStr1 = sourceStr.substring(28);
        String substr2 = sourceStr.substring(11,17);
        // %n是换行的格式字符串,只能用于print输出语句当中
        // \n也是换行的转义字符,可用于任何字符串
        System.out.printf("subStr1 == %s%n",subStr1);
        System.out.printf("subStr2 == %s%n",substr2);
        // 分割字符串
        String[] array = sourceStr.split(" ");
        System.out.println("调用Split()方法....");
        for (String item : array){
            System.out.printf("%s%n",item);
        }
    }
}
package chapter8;

public class chapt06 {
    public static void main(String[] args) {
        StringBuilder builder1 = new StringBuilder();
        System.out.println("length == " + builder1.length());
        System.out.println("capacity == " + builder1.capacity());

        StringBuilder builder2 = new StringBuilder("hello");
        System.out.println("length == " + builder2.length());
        System.out.println("capacity == " + builder2.capacity());
    }
    // 缓冲区的默认长度为16,超过容量会自动扩容
    StringBuilder builder3 = new StringBuilder();
}
package chapter8;

public class chapt07 {
    public static void main(String[] args) {
        StringBuilder s1 = new StringBuilder("Hello");
        s1.append(" ").append("world");
        s1.append(".");
        System.out.println(s1);
        // 添加布尔值 空对象 转义符
        StringBuilder s2 = new StringBuilder();
        Object object = null;
        // 布尔值false转换为"false" , null转换为"null"
        s2.append(false).append(‘\t‘).append(object);
        System.out.println(s2);
        // 添加数值
        StringBuilder s3 = new StringBuilder();
        for (int i = 0;i < 10; i++){
            s3.append(i);
        }
        System.out.println(s3);
    }
}
package chapter8;

public class chapt08 {
    public static void main(String[] args) {
        String str1 = "Java C";
        StringBuilder mstr = new StringBuilder(str1);
        // 插入字符串
        mstr.insert(4," C++");
        System.out.println(mstr);
        // 追加字符串
        mstr.insert(mstr.length()," Python");
        mstr.append(" PHP");
        System.out.println(mstr);
        // 删除字符串
        mstr.delete(11,23);
        System.out.println(mstr);
    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!