string - How to operate java string to achieve such effect
伊谢尔伦
伊谢尔伦 2017-05-17 09:59:34
0
3
539

For example, there are some strings like this:
H3C18,
H301C108
How to separate it into
H3 and C18,
H301 and C108

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
黄舟

Regular expressions seem to meet the needs

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        System.out.println(tokens("H3C18"));
        System.out.println(tokens("H301C108"));
    }

    private static final Pattern PATTERN = Pattern.compile("[a-zA-Z]\d+");

    public static List<String> tokens(String text) {
        List<String> result = new LinkedList<>();
        Matcher matcher = PATTERN.matcher(text);

        while (matcher.find()) {
            result.add(matcher.group(0));
        }

        return result;
    }
}

Output

[H3, C18]
[H301, C108]
仅有的幸福

The rule is "C", just make a fuss about it, it's not difficult, just think about it

洪涛

Please don’t be a douchebag. Such problems are all problems that can be solved by checking the API.
Think more and make more progress

The idea is to cut the string and find the index corresponding to the character.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!