Home >Java >javaTutorial >Summary of common string operations and date operations in Java
1. String operation
Create string
String s2 = new String("Hello World");
String s1 = "Hello World";
1. String connection
When multiple strings are linked, each string is connected with +, + is the string link, and a new string is generated after the connection.
2. Get the length of the string a.lenght()
Start from 0 according to the index, intercept the length of the string a.substring(1,3); intercept from position 1 to 3 Number position.
3. Get the index position of the specified string indexOf() method; lastIndexOf() method.
indexOf(s) return value: return the index of the first occurrence of s in the string
lastIndexOf(s) return value: return the index of the last occurrence of s in the string
4. Remove the spaces before and after the string trim()
##5. Replace all characters that match the specified string Matched string replace() method #6. Determine whether the strings are equal equals() methodUse equals() is strictly case-sensitive when comparing strings. Under this condition, if the two strings still have the same characters and length, it returns true, otherwise it returns false. 7. Determine the start of a string startsWith() method Determine the end of a string The endsWith() method method is used to determine whether the current string object starts or ends with the character specified by the parameter. 8. Case conversionConvert the uppercase letters in the string to lowercase toLowerCase() method;Convert the lowercase letters in the string to uppercase toUpperCase( ) method.9. String split split(String sign) methodThis method completely splits the string according to the specified delimiter. Homework:public static void main1 (String[] args){ String a = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" ; for(int i=0;i<4;i++){ int b = (int)(Math.random()*100)%a.length(); String c = a.substring(b, b+1); System.out.print(a.substring(b, b+1)); } } //随机生成不重复的四位数的验证码
public static void main(String[] args){ String a = " <student><xm>张三</xm><xb>男</xb></student>"; String b = a.substring(a.indexOf("<xm>")+4,a.indexOf("</xm>") ); System.out.println("姓名:"+b); String c =a.substring(a.indexOf("<xb>")+4,a.indexOf("</xb>") ); System.out.println("性别:"+c); } //截取姓名和性别
2. Date operation :Calendar(1) Read dateCalendar a = Calendar.getInstance();
d.get(constant);
d.get(Calendar.YEAR); //Return the digital year
d.get(Calendar.MONTH); //Month
d.get(Calendar.DAY_OF_MONTH); //Day
d.get(Calendar.HOUR); //Hour
d.get(Calendar.MINUTE);//Minutes
d.get(Calendar.SECOND);//Seconds
d.get(Calendar.MILLISECOND);//Milliseconds
yy,yyyy --year
M,MM--month
d, dd --day
h, hh--hour, 12-hour system; HH--hour, 24-hour system
m,mm-minute
s,ss-second
a.set(year, month, day);
a.set(year, month, day, hour, minute, second);
a.set(constant, value);
//c.set(1999,8,12);
//c.set(1999, 2,4,18,55,32);
//c.set(Calendar.YEAR, 1980);
public class Dog { public static void main(String[] args){ Calendar a = Calendar.getInstance(); a.set(2002, 2, 13); SimpleDateFormat b = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String c = b.format(a.getTime()); System.out.println(c); } //格式化日期显示 //时分秒不写是当前时间The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support PHP Chinese. net! For more related articles on common string operations and date operations in Java, please pay attention to the PHP Chinese website!