zoukankan      html  css  js  c++  java
  • Java_字符串操作

    1. 获取相关

    package cn.itcast.day08.demo02;
    
    /*
    String当中与获取相关的常用方法有:
    
    public int length():获取字符串当中含有的字符个数,拿到字符串长度。
    public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。
    public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始。)
    public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。
     */
    public class Demo02StringGet {
    
        public static void main(String[] args) {
            // 获取字符串的长度
            int length = "asdasfeutrvauevbueyvb".length();
            System.out.println("字符串的长度是:" + length);
    
            // 拼接字符串
            String str1 = "Hello";
            String str2 = "World";
            String str3 = str1.concat(str2);
            System.out.println(str1); // Hello,原封不动
            System.out.println(str2); // World,原封不动
            System.out.println(str3); // HelloWorld,新的字符串
            System.out.println("==============");
    
            // 获取指定索引位置的单个字符
            char ch = "Hello".charAt(1);
            System.out.println("在1号索引位置的字符是:" + ch);
            System.out.println("==============");
    
            // 查找参数字符串在本来字符串当中出现的第一次索引位置
            // 如果根本没有,返回-1值
            String original = "HelloWorldHelloWorld";
            int index = original.indexOf("llo");
            System.out.println("第一次索引值是:" + index); // 2
    
            System.out.println("HelloWorld".indexOf("abc")); // -1
        }
    
    }
    

    2. 字符串的截取方法

    package cn.itcast.day08.demo02;
    
    /*
    字符串的截取方法:
    
    public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。
    public String substring(int begin, int end):截取从begin开始,一直到end结束,中间的字符串。
    备注:[begin,end),包含左边,不包含右边。
     */
    public class Demo03Substring {
    
        public static void main(String[] args) {
            String str1 = "HelloWorld";
            String str2 = str1.substring(5);
            System.out.println(str1); // HelloWorld,原封不动
            System.out.println(str2); // World,新字符串
            System.out.println("================");
    
            String str3 = str1.substring(4, 7);
            System.out.println(str3); // oWo
            System.out.println("================");
    
            // 下面这种写法,字符串的内容仍然是没有改变的
            // 下面有两个字符串:"Hello","Java"
            // strA当中保存的是地址值。
            // 本来地址值是Hello的0x666,
            // 后来地址值变成了Java的0x999
            String strA = "Hello";
            System.out.println(strA); // Hello
            strA = "Java";
            System.out.println(strA); // Java
        }
    
    }
    

    3. 转换相关

    package cn.itcast.day08.demo02;
    
    /*
    String当中与转换相关的常用方法有:
    
    public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值。
    public byte[] getBytes():获得当前字符串底层的字节数组。
    public String replace(CharSequence oldString, CharSequence newString):
    将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串。
    备注:CharSequence意思就是说可以接受字符串类型。
     */
    public class Demo04StringConvert {
    
        public static void main(String[] args) {
            // 转换成为字符数组
            char[] chars = "Hello".toCharArray();
            System.out.println(chars[0]); // H
            System.out.println(chars.length); // 5
            System.out.println("==============");
    
            // 转换成为字节数组
            byte[] bytes = "abc".getBytes();
            for (int i = 0; i < bytes.length; i++) {
                System.out.println(bytes[i]);
            }
            System.out.println("==============");
    
            // 字符串的内容替换
            String str1 = "How do you do?";
            String str2 = str1.replace("o", "*");
            System.out.println(str1); // How do you do?
            System.out.println(str2); // H*w d* y*u d*?
            System.out.println("==============");
    
            String lang1 = "会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!";
            String lang2 = lang1.replace("你大爷的", "****");
            System.out.println(lang2); // 会不会玩儿呀!****!****!****!!!
        }
    
    }
    

    4. 分割字符串

    package cn.itcast.day08.demo02;
    
    /*
    分割字符串的方法:
    public String[] split(String regex):按照参数的规则,将字符串切分成为若干部分。
    
    注意事项:
    split方法的参数其实是一个“正则表达式”,今后学习。
    今天要注意:如果按照英文句点“.”进行切分,必须写"\."(两个反斜杠)
     */
    public class Demo05StringSplit {
    
        public static void main(String[] args) {
            String str1 = "aaa,bbb,ccc";
            String[] array1 = str1.split(",");
            for (int i = 0; i < array1.length; i++) {
                System.out.println(array1[i]);
            }
            System.out.println("===============");
    
            String str2 = "aaa bbb ccc";
            String[] array2 = str2.split(" ");
            for (int i = 0; i < array2.length; i++) {
                System.out.println(array2[i]);
            }
            System.out.println("===============");
    
            String str3 = "XXX.YYY.ZZZ";
            String[] array3 = str3.split("\.");
            System.out.println(array3.length); // 0
            for (int i = 0; i < array3.length; i++) {
                System.out.println(array3[i]);
            }
        }
    
    }
    
  • 相关阅读:
    HDU2059(龟兔赛跑)
    pat 1012 The Best Rank
    pat 1010 Radix
    pat 1007 Maximum Subsequence Sum
    pat 1005 Sign In and Sign Out
    pat 1005 Spell It Right
    pat 1004 Counting Leaves
    1003 Emergency
    第7章 输入/输出系统
    第六章 总线
  • 原文地址:https://www.cnblogs.com/yzg-14/p/12594595.html
Copyright © 2011-2022 走看看