1. trim() method
The trim() method is used to remove the leading and trailing whitespace characters of a string.
Example:
public class Test { public static void main(String args[]) { String Str = new String(" www.runoob.com "); System.out.print("原始值 :" ); System.out.println( Str ); System.out.print("删除头尾空白 :" ); System.out.println( Str.trim() ); } }
Result:
原始值 : www.runoob.com 删除头尾空白 :www.runoob.com
2. split() method
split( ) method splits a string based on matching the given regular expression.
Note: Escape characters such as ., | and * must be added with \\.
Note: Multiple separators, you can use | as a hyphen.
Syntax:
public String[] split(String regex, int limit)
Parameters:
##regex -- Regular expression delimiter.
limit -- The number of divided parts.
Return value:
String array.3. charAt() method
The charAt() method is used to return the character at the specified index. The index range is from 0 to length() - 1.Syntax:
public char charAt(int index)
Parameters:
index -- The index of the character.
Return value:
Returns the character at the specified index. Instance:public class Test { public static void main(String args[]) { String s = "www.runoob.com"; char result = s.charAt(8); System.out.println(result); } }Result:
4. substring() method
Syntax:
public String substring(int beginIndex) 或 public String substring(int beginIndex, int endIndex)
Parameters:
beginIndex<span style="color: rgb(20, 25, 30); font-family: " microsoft yahei sans gb helvetica neue tahoma arial sans-serif font-size:></span>
-- Starting index (inclusive), index starts from 0.
endIndex -- End index (not included).
Return value
Substring. Example:public class Test { public static void main(String args[]) { String Str = new String("www.runoob.com"); System.out.print("返回值 :" ); System.out.println(Str.substring(4) ); System.out.print("返回值 :" ); System.out.println(Str.substring(4, 10) ); } }Result:
返回值 :runoob.com 返回值 :runoobRecommended tutorial:
The above is the detailed content of What are the commonly used methods in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.