字符串

作者: Mtllll | 来源:发表于2019-03-06 21:19 被阅读0次


/*
 * 看程序写结果
 * 字符串如果是变量相加,先开空间,在拼接。
 * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
 */
public class StringDemo4 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        System.out.println(s3 == s1 + s2);// false。字符串如果是变量相加,先开空间,再拼接。
        System.out.println(s3.equals((s1 + s2)));// true

        System.out.println(s3 == "hello" + "world");//true。字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
        System.out.println(s3.equals("hello" + "world"));// true

        // 通过反编译看源码,我们知道这里已经做好了处理。
        // System.out.println(s3 == "helloworld");
        // System.out.println(s3.equals("helloworld"));
    }
}

String的判断功能:

*
 * String类的判断功能:
 * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
 * boolean contains(String str):判断大字符串中是否包含小字符串
 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
 * boolean isEmpty():判断字符串是否为空。
 * 
 * 注意:
 *         字符串内容为空和字符串对象为空。
 *         String s = "";//对象存在,所以可以调方法
 *         String s = null;//对象不存在,不能调方法
 */
public class StringDemo {
    public static void main(String[] args) {
        // 创建字符串对象
        String s1 = "helloworld";
        String s2 = "helloworld";
        String s3 = "HelloWorld";

        // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
        System.out.println("equals:" + s1.equals(s2));
        System.out.println("equals:" + s1.equals(s3));
        System.out.println("-----------------------");

        // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
        System.out.println("equals:" + s1.equalsIgnoreCase(s2));
        System.out.println("equals:" + s1.equalsIgnoreCase(s3));
        System.out.println("-----------------------");

        // boolean contains(String str):判断大字符串中是否包含小字符串
        System.out.println("contains:" + s1.contains("hello"));
        System.out.println("contains:" + s1.contains("hw"));
        System.out.println("-----------------------");

        // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
        System.out.println("startsWith:" + s1.startsWith("h"));
        System.out.println("startsWith:" + s1.startsWith("hello"));
        System.out.println("startsWith:" + s1.startsWith("world"));
        System.out.println("-----------------------");

        // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩

        // boolean isEmpty():判断字符串是否为空。
        System.out.println("isEmpty:" + s1.isEmpty());

        String s4 = "";
        String s5 = null;
        System.out.println("isEmpty:" + s4.isEmpty());
        // NullPointerException
        // s5对象都不存在,所以不能调用方法,空指针异常
//        System.out.println("isEmpty:" + s5.isEmpty());
        
    }
}

String的获取功能

*
 * String类的获取功能
 * int length():获取字符串的长度。
 * char charAt(int index):获取指定索引位置的字符
 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
 *         为什么这里是int类型,而不是char类型?
 *         原因是:'a'和97其实都可以代表'a'。如果里面写char,就不能写数字97了
 * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
 * String substring(int start):从指定位置开始截取字符串,默认到末尾。
 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
 */
public class StringDemo {
    public static void main(String[] args) {
        // 定义一个字符串对象
        String s = "helloworld";

        // int length():获取字符串的长度。
        System.out.println("s.length:" + s.length());//10
        System.out.println("----------------------");

        // char charAt(int index):获取指定索引位置的字符
        System.out.println("charAt:" + s.charAt(7));//
        System.out.println("----------------------");

        // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
        System.out.println("indexOf:" + s.indexOf('l'));
        System.out.println("----------------------");

        // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
        System.out.println("indexOf:" + s.indexOf("owo"));
        System.out.println("----------------------");

        // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
        System.out.println("indexOf:" + s.indexOf('l', 4));
        System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
        System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
        System.out.println("----------------------");

        // 自己练习:int indexOf(String str,int
        // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

        // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
        System.out.println("substring:" + s.substring(5));
        System.out.println("substring:" + s.substring(0));
        System.out.println("----------------------");

        // String substring(int start,intend):从指定位置开始到指定位置结束截取字符串。
        //包括start索引但是不包end索引
        System.out.println("substring:" + s.substring(3, 8));
        System.out.println("substring:" + s.substring(0, s.length()));
    }
}

String的转换功能

/*
 * String的转换功能:
 * byte[] getBytes():把字符串转换为字节数组。
 * char[] toCharArray():把字符串转换为字符数组。
 * static String valueOf(char[] chs):把字符数组转成字符串。
 * static String valueOf(int i):把int类型的数据转成字符串。
 *         注意:String类的valueOf方法可以把任意类型的数据转成字符串。
 * String toLowerCase():把字符串转成小写。
 * String toUpperCase():把字符串转成大写。
 * String concat(String str):把字符串拼接。
 */
public class StringDemo {
    public static void main(String[] args) {
        // 定义一个字符串对象
        String s = "JavaSE";

        // byte[] getBytes():把字符串转换为字节数组。
        byte[] bys = s.getBytes();
        for (int x = 0; x < bys.length; x++) {
            System.out.println(bys[x]);
        }
        System.out.println("----------------");

        // char[] toCharArray():把字符串转换为字符数组。
        char[] chs = s.toCharArray();
        for (int x = 0; x < chs.length; x++) {
            System.out.println(chs[x]);
        }
        System.out.println("----------------");

        // static String valueOf(char[] chs):把字符数组转成字符串。
        String ss = String.valueOf(chs);
        System.out.println(ss);
        System.out.println("----------------");

        // static String valueOf(int i):把int类型的数据转成字符串。
        int i = 100;
        String sss = String.valueOf(i);
        System.out.println(sss);
        System.out.println("----------------");

        // String toLowerCase():把字符串转成小写。
        System.out.println("toLowerCase:" + s.toLowerCase());
        System.out.println("s:" + s);
        // System.out.println("----------------");
        // String toUpperCase():把字符串转成大写。
        System.out.println("toUpperCase:" + s.toUpperCase());
        System.out.println("----------------");

        // String concat(String str):把字符串拼接。
        String s1 = "hello";
        String s2 = "world";
        String s3 = s1 + s2;
        String s4 = s1.concat(s2);
        System.out.println("s3:"+s3);
        System.out.println("s4:"+s4);
    }
}
public class StringTest {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "helloWORLD";

        // 先获取第一个字符
        String s1 = s.substring(0, 1);
        // 获取除了第一个字符以外的字符
        String s2 = s.substring(1);
        // 把A转成大写
        String s3 = s1.toUpperCase();
        // 把B转成小写
        String s4 = s2.toLowerCase();
        // C拼接D
        String s5 = s3.concat(s4);
        System.out.println(s5);

        // 优化后的代码
        // 链式编程
        String result = s.substring(0, 1).toUpperCase()
                .concat(s.substring(1).toLowerCase());
        System.out.println(result);

String类的其他功能:
替换功能
去除字符串两空格
按字典顺序比较两个字符串

public class StringDemo {
    public static void main(String[] args) {
        // 替换功能
        String s1 = "helloworld";
        String s2 = s1.replace('l', 'k');
        String s3 = s1.replace("owo", "ak47");
        System.out.println("s1:" + s1);
        System.out.println("s2:" + s2);
        System.out.println("s3:" + s3);
        System.out.println("---------------");

        // 去除字符串两空格
        String s4 = " hello world  ";
        String s5 = s4.trim();
        System.out.println("s4:" + s4 + "---");
        System.out.println("s5:" + s5 + "---");

        // 按字典顺序比较两个字符串
        String s6 = "hello";
        String s7 = "hello";
        String s8 = "abc";
        String s9 = "xyz";
        System.out.println(s6.compareTo(s7));// 0
        System.out.println(s6.compareTo(s8));// 7
        System.out.println(s6.compareTo(s9));// -16
    }
}

字符串反转

public class StringTest3 {
    public static void main(String[] args) {
        // 键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        String s = myReverse(line);
        System.out.println("实现功能后的结果是:" + s);
    }

    /*
     * 两个明确: 返回值类型:String 参数列表:String
     */
    public static String myReverse(String s) {
        // 定义一个新字符串
        String result = "";

        // 把字符串转成字符数组
        char[] chs = s.toCharArray();

        // 倒着遍历字符串,得到每一个字符
        for (int x = chs.length - 1; x >= 0; x--) {
            // 用新字符串把每一个字符拼接起来
            result += chs[x];
        }
        return result;
    }
}

String类提供了split()方法,将一个字符串分割为子字符串,结果作为字符串数组返回

public class Lyric {
        public static void main(String[] args) {
            String words="长亭外 古道边 芳草碧连天 晚风扶 柳笛声残 夕阳山外山";
            String[ ] printword=new String[100];        
            System.out.println("***原歌词格式***\n"+words);
            System.out.println("\n***拆分后歌词格式***");
            printword=words.split(" "); 
            for(int i=0;i<printword.length;i++){
                System.out.println( printword[i] );
            }
        }
}
问题:字符串str中java出现的次数
a.String str="jfoijdcionjavajfipdsvmiiojavaiojicovjasduvnjavafds";
        String str1="java";
        int count=0;
        while(str.indexOf(str1)!=-1){
            count++;
            str=str.substring(str.indexOf(str1)+str1.length());
        }
        System.out.println(count);
b. String []str3=str.split(str1);
        System.out.println(str3.length-1);

相关文章

  • Javascript知识点整合

    字符串 单行字符串: ‘字符串’或“字符串” 多行字符串: `多行字符串` 字符串操作: 字符串连接‘+’号 长度...

  • C++基础字符串

    字符串的构造 字符串特性描述 字符操作 字符串赋值 字符串连接 字符串比较 字符串查找 字符串替换 字符串删除 字...

  • iOS中的NSString与NSMutableString

    字符串的创建 字符串读写 字符串的比较 字符串的搜索 字符串截取 字符串替换 字符串与路径 字符串转换 NSMut...

  • iOS NSString用法总结

    字符串属性 字符串截取 字符串比较 字符串搜索 字符串拼接 字符串基本类型转换 字符串分行,分段 字符串列举(按条...

  • php 字符串常见方法汇总

    字符串拼接 字符串检索 字符串截取 字符串替换 字符串大小写转化 字符串转数组 字符串格式化

  • iOS 字符串截取、iOS 字符串替换、iOS 字符串分隔、iO

    iOS之字符串截取、iOS 字符串替换、iOS字符串分隔、iOS之字符串匹配、截取字符串、匹配字符串、分隔字符串 ...

  • PHP中字符串函数库常用函数解析 -- PHP 学习 (十一)

    常用字符串函数分类: 字符串长度, 字符串查找, 字符串大小写转换, 字符串截取, 字符串 ASCII, 字符串加...

  • Kotlin语言(二):字符串类型

    1、字符串定义 2、字符串删除空格 3、字符串比较 4、字符串切割 5、字符串截取 6、字符串替换 7、字符串模板

  • 字符串扩展

    求字符串大小 字符串解码、转换 字符串截取 字符串汉字处理 字符串 Mac地址 字符串进制转换

  • 2020-09-30字符串

    day8-字符串 字符串的操作 in 和 not in字符串1 in 字符串2 - 判断字符串1是否是字符串...

网友评论

      本文标题:字符串

      本文链接:https://www.haomeiwen.com/subject/auzjgxtx.html