java.lang.String和正则表达式

1.String类简介

String类中相关方法的使用
String[] split​(String regex)regex:正则表达式Splits this string around matches of the given regular expression.
String[] split​(String regex, int limit) limit代表什么Splits this string around matches of the given regular expression
char[] toCharArray()Converts this string to a new character array
String toLowerCase​(Locale locale)Converts all of the characters in this String to lower case using the rules of the given Locale.
String toLowerCase​()Converts all of the characters in this String to lower case using the rules of the default locale. 对应的还有:toUpperCase
String valueOf(java中提供的所有基本类型)得到一个字符串,和String类的构造函数结合起来理解
String substring​(int beginIndex)Returns a string that is a substring of this string
String substring​(int beginIndex, int endIndex)Returns a string that is a substring of this string 字符串的长度为endindex-beginindex,因此beginindex可以比字符串的长度加1
char charAt​(int index)Returns the char value at the specified index

正则表达式
Java中String.split()分割字符串细节问题小结

一个使用split()的例子(2019蓝桥杯javaB组省赛 )

人物相关性分析

public static void main(String[] args) {
split("\\.")是什么意思
[原文链接](https://www.cnblogs.com/ylht/p/10249543.html)
        Scanner input = new Scanner(System.in);
        try {
            int K = input.nextInt();
            input.nextLine();
            String text = input.nextLine();
            System.out.println("s.length="+ text.length());

            // 字符串分割,按照空格和.分割字符,若是(.空格)分割后为空字符串。\\s \\s+ \\.
            String[] words = text.split("\\s|\\.");
            // String[] words = text.split("\\s|\\."); 加不加“+” , 效果相同

            int[] wordsLength = new int[words.length]; // 将分割的字符串的长度值存储,避免三重循环中调用String.length();
            for (int i = 0; i < words.length; i++) {
                wordsLength[i] = words[i].length();
            }

            int ans = 0;

            // Alice ——> Bob的距离
            for (int i = 0; i < words.length; i++) {
                if (words[i].equals("Alice")) {
                    for (int j = i + 1; j < words.length; j++) {
                        int sum = 1;
                        if (words[j].equals("Bob")) {
                            for (int k = i + 1; k < j; k++) {
                                // 每个单词的长度加空格占据的长度
                                sum += wordsLength[k] + 1;
                            }
                            if (sum <= K) {
                                ans++;
                            }
                        }
                    }
                }
            }
            // Bob ——> Alice的距离
            for (int i = 0; i < words.length; i++) {
                if (words[i].equals("Bob")) {
                    for (int j = i + 1; j < words.length; j++) {
                        int sum = 1;
                        if (words[j].equals("Alice")) {
                            for (int k = i + 1; k < j; k++) {
                                // 每个单词的长度加空格占据的长度
                                sum += wordsLength[k] + 1;
                            }
                            if (sum <= K) {
                                ans++;
                            }
                        }
                    }
                }
            }
            System.out.println(ans);
        } catch (Exception e) {
            input.close();
        }
    }
posted @ 2021-02-07 20:27  木有呂朋友  阅读(36)  评论(0编辑  收藏  举报