Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of the String class

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what are the common methods of String". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

String class

A string is a constant and cannot be changed after creation

String literals are stored in a string pool and can be shared.

String s = "Hello"; generate an object that is stored in the string pool

String s = new String ("Hello"); / / two objects are generated, one for each heap and one for the pool

Common methods

Public int length (): returns the string length

String str = "java is the best language"; System.out.println (str.length ()); / / get the string length

Public char charAt (int index): get characters based on subscript

String str = "java is the best language in the world"; System.out.println (str.charAt (str.length ()-1)); / / get the last bit of the string

Public boolean contains (String str): determines whether the current string contains str

String str = "java is the best language in the world"; System.out.println (str.contains ("java")); / / check to see if the string contains "java".

Public char [] toStringArray (): converts strings to arrays

Public int indexOf (String str): finds the subscript that occurs for the first time in str, and returns it if it exists, or-1 if it does not exist.

Public int lastIndexOf (String str): finds the last subscript index of a string in the current string

Public String trim (): remove the space before and after the string

Public String toUpperCase (): converts lowercase to uppercase

Public boolean endWith (String str): determines whether a string ends with str

Public boolean startWith (String str): determines whether a string begins with str

Public String replace (char oldChar,char newChar); replace the old string with the new string

Public String [] split (String str): split based on str

String say = "java is the best progaming language,java xiang"; String [] arr = say.split ("[,] +"); System.out.println (arr.length); for (String string: arr) {System.out.println (string);}

Public String subString (): intercepts a string

Variable string

StringBuffer: variable length string, provided by JDK1.0, slow and thread safe

StringBuilder: variable length string, provided by JDK5.0, with fast running efficiency and thread safety.

Append (); append: append to the end of a string

Insert (); add: add at specified location

Replace (); replace: replace at a specified location

Delete ();: delete

Public class TestDemo01 {public static void main (String [] args) {StringBuffer sb = new StringBuffer (); / / 1 append (); add sb.append ("java World first"); System.out.println (sb.toString ()); sb.append ("Zhenxiang"); System.out.println (sb.toString ()); / / 2 insert () Add sb.insert (0, "front:"); System.out.println (sb.toString ()); sb.replace (0Pol 3, "start:"); / / replace System.out.println (sb.toString ()); sb.delete (0P3); / / remove System.out.println (sb.toString ());}}

StringBuilder is more efficient than StringBuffer

Public class TestDemo02 {public static void main (String [] args) {long start = System.currentTimeMillis (); StringBuffer sb = new StringBuffer (); for (int I = 0; I

< 99999; i++){ sb.append(i); } long end = System.currentTimeMillis(); System.out.println("StringBuffer用时:"+(end - start)); System.out.println("============================="); long start1 = System.currentTimeMillis(); StringBuilder sb1 = new StringBuilder(); for (int i = 0; i < 99999; i++) { sb1.append(i); } long end1 = System.currentTimeMillis(); System.out.println("StringBuilder用时:"+(end1 - start1)); if((end-start)>

(end1-start1) {System.out.println ("StringBuilder is more efficient than StringBuffer");} else {System.out.println ("StringBuffer is more efficient than StringBuilder");}} BigDecimal

Many practical applications need accurate operation, but double is the approximate value storage, which no longer meets the requirements, and needs the help of BigDecimal

Location: in the java.math package

Function: accurately calculate floating point numbers

Creation method: BigDecimal bd = new BigDecimal ("1.0")

Methods:

BigDecimal add (BigDecimal bd) plus

BigDecimal subtract (BigDecimal bd) minus

BigDecimal multiply (BigDecimal bd) multiplication

BigDecimal divide (BigDecimal bd) division

Import java.math.BigDecimal;public class TestDemo03 {public static void main (String [] args) {double D1 = 1.0; double D2 = 0.9; System.out.println (D1-D2); / / BigDecimal, large floating-point numbers accurately calculate BigDecimal bd1 = new BigDecimal ("1.0"); BigDecimal bd2 = new BigDecimal (" 0.9 "); / / subtract subtract () BigDecimal R1 = bd1.subtract (bd2); System.out.println (R1); / / addition add (); BigDecimal R2 = bd1.add (bd2); System.out.println (R2); / / multiplicative multiply (); BigDecimal R3 = bd1.multiply (bd2); System.out.println (R3); / / division divide () BigDecimal R4 = new BigDecimal ("1 4") .subtract (new BigDecimal ("0 5"). New BigDecimal ("0 9")); / / (1 4-0 5) / 0 9 System.out.println (R4);}} Date

Date represents a specific moment, accurate to milliseconds. Most of the methods in the Date class have been replaced by methods in the Calendar class

Time unit

1 second = 1000 Ms

1 millisecond = 1000 microsecond

1 microsecond = 1000 nanosecond

Calendar

Calendar provides methods to get or set various calendar fields

Construction method

Protected Calendar (): because the modifier is protected, the object cannot be created directly

Other methods

The method name indicates that static Calendar getInstance () uses the default time zone and region to get calendar void set (int year,int month,int date,int hourofday,int minute,int second) to set the calendar's year, month, day, hour, minute, second int get (int field) to return the value of a given calendar field, and fields such as year, month, day, etc. Void setTime (Date date) sets the time of this calendar with a given Date. Date-CalendarDate getTime () returns a Date that represents the time of this calendar. Calendar-Datevoid add (int field, int amount) adds or decreases the amount of time to a specified field according to the rules of the calendar. Long getTimeInMillies () milliseconds returns the time value SimpleDateFormat of the calendar.

SimpleDateFormat is a concrete class that formats and parses dates in a locale-dependent way

Format (date-> text), parse (text-- > date)

Commonly used time pattern letters

Letter date or time sample yyear 2019M Mid-month 08d Mid-day 10H1 hours in days (0-23) 22m min 16s seconds 59s Ms 367public static void main (String [] args) throws Exception {/ / create SimpleDateFormat object y M / month SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd"); / / create Date object Date date = new Date () / / format Date (convert date to string) String str = sdf.format (date); System.out.println (str); / / parse (convert string to date) Date date2 = sdf.parse ("1999-02-24"); System.out.println (date2);} System class

System system class, mainly used to obtain system attribute data and other operations, constructor private

The method name describes static void arraycopy (...). Copy the array static long currentTimeMillis (); get the current system time and return the millisecond value static void gc (); it is recommended that JVM quickly start the garbage collector to collect garbage static void exit (int status); exit jvm, if the parameter is 0 means normal exit jvm, non-zero indicates abnormal exit jvm "what are the common methods of String class" here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report