Home  >  Article  >  Java  >  Summary of common string operations and date operations in Java

Summary of common string operations and date operations in Java

高洛峰
高洛峰Original
2017-01-20 16:40:031257browse

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()

Summary of common string operations and date operations in JavaSummary of common string operations and date operations in Java

##5. Replace all characters that match the specified string Matched string replace() method

Summary of common string operations and date operations in JavaSummary of common string operations and date operations in Java

#6. Determine whether the strings are equal equals() method

Use 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 conversion

Convert 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) method

This 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));
   
 }
 }
//随机生成不重复的四位数的验证码

Summary of common string operations and date operations in Java

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);
  
 }
 //截取姓名和性别

Summary of common string operations and date operations in Java

2. Date operation :Calendar

(1) Read date

Calendar 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

Formatted display of date:

Use date formatting display SimpleDateFormat

1. Create a date Calendar

Calendar a = Calendar.getInstance();

2. Create a formatter

SimpleDateFormat f = new SimpleDateFormat("Formatting style");

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

3. Format calendar

f.format(date); //Note that it is Date, not Calendar;

f.format(a.getTime()); //Use Calendar object getTime() function, convert into Date object

(2) Write date

Calendar a = Calendar.getInstance();

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);
 }
//格式化日期显示
//时分秒不写是当前时间

Summary of common string operations and date operations in Java

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!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn