277、Java基础53 - 数字与字符串【字符串】 2019.11.21

1、创建字符串

字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象
常见创建字符串手段:

  1. 每当有一个字面值出现的时候,虚拟机就会创建一个字符串
  2. 调用String的构造方法创建一个字符串对象
  3. 通过+加号进行字符串拼接也会创建新的字符串对象
package character;
 
public class TestString {
 
    public static void main(String[] args) {
        String garen ="盖伦"; //字面值,虚拟机碰到字面值就会创建一个字符串对象
         
        String teemo = new String("提莫"); //创建了两个字符串对象
         
        char[] cs = new char[]{'崔','斯','特'};
         
        String hero = new String(cs);//  通过字符数组创建一个字符串对象
         
        String hero3 = garen + teemo;//  通过+加号进行字符串拼接
    }
}

2、final

String 被修饰为final,所以是不能被继承的

package character;
 
public class TestString {
 
    public static void main(String[] args) {
        MyString str = new MyString();
         
    }
     
        /*这里会报错,因为String不能被继承*/
    static class MyString extends String{
         
    }
     
}

3、immutable

immutable 是指不可改变的
比如创建了一个字符串对象
String garen =“盖伦”;
不可改变的具体含义是指:
不能增加长度
不能减少长度
不能插入字符
不能删除字符
不能修改字符
一旦创建好这个字符串,里面的内容 永远 不能改变

String 的表现就像是一个常量

package character;
  
public  class TestString {
  
    public static void main(String[] args) {
        String garen ="盖伦";
         
    }
}

4、字符串格式化

如果不使用字符串格式化,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐
使用字符串格式化,就可以简洁明了

package character;
   
public class TestString {
   
    public static void main(String[] args) {
  
        String name ="盖伦";
        int kill = 8;
        String title="超神";
          
        //直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
        String sentence = name+ " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号";
          
        System.out.println(sentence);
         
        //格式化字符串
        //%s表示字符串,%d表示数字,%n表示换行
        String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
         
        String sentence2 = String.format(sentenceFormat, name,kill,title);
         
        System.out.println(sentence2);
         
    }
}

5、字符串长度

length方法返回当前字符串的长度
可以有长度为0的字符串,即空字符串

package character;
   
public class TestString {
   
    public static void main(String[] args) {
  
        String name ="盖伦";
         
        System.out.println(name.length());
         
        String unknowHero = "";
         
        //可以有长度为0的字符串,即空字符串
        System.out.println(unknowHero.length());
          
    }
}

6、练习:随机字符串

创建一个长度是5的随机字符串,随机字符有可能是数字,大写字母或者小写字母

给点提示: 数字和字符之间可以通过互相转换

char c = 'A';
short s = (short) c;

通过这个手段就能够知道字符 a-z A-Z 0-9 所对应的数字的区间了

扫描二维码关注公众号,回复: 10821260 查看本文章

需要用到ASCII码对照表

7、练习:字符串数组排序

创建一个长度是8的字符串数组
使用8个长度是5的随机字符串初始化这个数组
对这个数组进行排序,按照每个字符串的首字母排序(无视大小写)

注1: 不能使用Arrays.sort() 要自己写
注2: 无视大小写,即 Axxxx 和 axxxxx 没有先后顺序

8、练习:穷举法破解密码

  1. 生成一个长度是3的随机字符串,把这个字符串作为当做密码

  2. 使用穷举法生成长度是3个字符串,匹配上述生成的密码

要求: 分别使用多层for循环 和 递归解决上述问题

package Digit;
 
 
import java.util.Arrays;
import java.util.Random;
 
public class Practice {
    static char getRandomChar()
    {
        int random = new Random().nextInt(62);
        if(random >= 0 && random <=9) 
            return (char)(random+48);
        else if(random >= 10 && random <= 35)
            return (char)(random+55);
        else
            return (char)(random+61);
    }
     
    static String getRandomString(int len)
    {
        String str = "";
        for(int i = 0;i<len;i++) {
            str = str+getRandomChar();
        }
        return str;
    }
     
    static void sortRandomString(String[] str)
    {
        for(int i= 0 ;i<str.length-1;i++) {
            for(int j = i+1 ; j < str.length ; j++) {
                char c1 = Character.toLowerCase(str[i].charAt(0));
                char c2 = Character.toLowerCase(str[j].charAt(0));
                if(c1>c2) {
                    String tmp = str[i];
                    str[i] = str[j];
                    str[j] = tmp;
                }
            }
        }
    }
     
    static String findCode(String code)
    {
        char[]table = new char[62];
        for(int i = 0 ; i <= 9 ; i++) {
            table[i] = (char)(i+48);
        }
        for(int i = 10 ; i <=35 ; i++) {
            table[i] = (char)(i+55);
        }
        for(int i = 36 ; i < 61 ; i++) {
            table[i] = (char)(i + 61);
        }
         
        for(char c0:table) {
            if(c0 == code.charAt(0))
            {
                for(char c1 : table) {
                    if(c1 == code.charAt(1))
                    {
                        for(char c2 : table) {
                            if(c2 == code.charAt(2)) {
                                char ans[] = {c0,c1,c2};
                                return Arrays.toString(ans);
                            }
                        }
                    }
                }
            }
        }
         
        return "";
    }
     
    public static void main(String[] args) {
        //Test1:创建一个长度是5的随机字符串,随机字符有可能是数字,大写字母或者小写字母
        String s = getRandomString(5);
        System.out.println("Test1:");
        System.out.println(s);
         
        /**
          *Test2:
            *创建一个长度是8的字符串数组
            *使用8个长度是5的随机字符串初始化这个数组
            *对这个数组进行排序,按照每个字符串的首字母排序(无视大小写)
         */
        String[] str = new String[8];
        for(int i = 0 ; i < str.length; i++) {
            str[i] = getRandomString(5);
        }
        System.out.println("\nTest2:");
        System.out.println("Before sort:"+Arrays.toString(str));
        sortRandomString(str);
        System.out.println("After sort:"+Arrays.toString(str));
         
        /**
         * Test3:
         * 1. 生成一个长度是3的随机字符串,把这个字符串作为当做密码
         * 2. 使用穷举法生成长度是3个字符串,匹配上述生成的密码
         */
        String code = getRandomString(3);
        System.out.println("\nTest3:");
        System.out.println("code:"+code);
        System.out.println(findCode(code));
    }
}

9、参考链接

[01] How2j - 数字与字符串系列教材 (六)- JAVA中的字符串STRING详解

发布了309 篇原创文章 · 获赞 229 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/youyouwuxin1234/article/details/103190285