For example, there are some strings like this: H3C18, H301C108How to separate it into H3 and C18, H301 and C108
小伙看你根骨奇佳,潜力无限,来学PHP伐。
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.
Regular expressions seem to meet the needs
Output
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.