Home > Back-end >  Java beginners if the input line of data, how to separate the Numbers and symbols
Java beginners if the input line of data, how to separate the Numbers and symbols

Time:10-01

As title, suppose I am in a line of input 2020/3/5 5 8
How can I take out these data? Such as I want a year=2020 month=3, etc.,
That how do such input data, and what to do,
String Builder can do, what to write, thank bosses,

CodePudding user response:

Regular expression
For example
 import Java. Util. Regex. Matcher. 
Import the Java. Util. Regex. The Pattern;

Public class Sample {
Public static void main (String [] args) {
String data="https://bbs.csdn.net/topics/2020/3/5 5 8";
String reg="(\ \ d {4})/(\ \ d {1, 2})/(\ \ d {1, 2})";
The Matcher m=Pattern.com running (reg.) the Matcher (data);
While (m. ind ()) {
System. The out. Printf (" year=% s \ n ", m.g roup (1));
System. The out. Printf (" the month=% s \ n ", m.g roup (2));
System. The out. Printf (" day=% s \ n ", m.g roup (3));
}
}
}

CodePudding user response:

Feel your business is not reasonable! If only for the date, the input is a string, and format must be fixed, such as the 2020-1-1 00:00:00, unity is the input format, can be parsed into with SimpleDateFormat date, so we can respectively obtain the corresponding date and time, can also use the split () method to cut into the array
  • Related