trim String class () to explain

tags: jobs

[quote = "javaeye"] [size = medium] because it is a web crawling work, quite often it is the processing of the string. JAVA is so commonly used classes in the String class, Pattern class, Matcher the class. trim where the String class () matches the methods and Matcher class () method, let's say something about their role and local attention.
[/size][/quote]


The following test class code

class A{

public static void main(String[] args) {
A a=new A();
a.testTrim();
a.testRegex();
}

public void testTrim(){
String str[]=new String[4];
str[0]=" this is test method trim ";
str[1]=" this is test method trim ";
str[2]="\t this is test method trim \t";
str[3]=" \r\n this is test method trim \r\n\t ";
System.out.println ( "Here is detected in String trim () function removes those characters: \ n");
for (int i = 0; i < str.length; i++) {
System.out.println("|"+str[i].trim()+"|");
}
}

public void testRegex(){
String str[]=new String[4];
str[0]="\r\naaaaAAaaa\r\n";
str[1]=" aaaaBBaaa ";
str[2]=" aaaaCCaaa ";
str[3]="\taaaaDDaaa\t";
Pattern p=Pattern.compile(".*([A-Z]{2}).*");
System.out.println ( "\ n each array element is not added trim () is a function of the result after: \ n");
for (int i = 0; i < str.length; i++) {
Matcher matcher=p.matcher(str[i]);
if(matcher.matches()){
System.out.println ( "first match" + i + "element, its value is:" + matcher.group (1));
}
else{
System.out.println ( "Matching" + i + "failed element");
}
}
System.out.println ( "\ n plus each array element trim () is a function of the result after: \ n");
for (int i = 0; i < str.length; i++) {
Matcher matcher=p.matcher(str[i].trim());
if(matcher.matches()){
System.out.println ( "first match" + i + "element, its value is:" + matcher.group (1));
}
else{
System.out.println ( "Matching" + i + "failed element");
}
}
}
}

// Here are the results of running the code:

Here is to detect the String trim () function removes those characters:

| this is test method trim |
|this is test method trim|
|this is test method trim|
|this is test method trim|

Not every array element plus trim () is a function of the result after:

0 matching element failure
The first matching element, which value is: BB
The second matching element, which value is: CC
The third matching element, which value is: DD

Plus trim on each array element () is a function of the result after:

0 matched elements whose value is: AA
The first matching element, which value is: BB
The second matching element, which value is: CC
The third matching element, which value is: DD



[Quote = "javaeye"] [size = medium] From the above results, we can see:

[Color = red] 1.trim () method is a full-size space can not be removed at both ends of the string, i.e., Chinese space

2.matches string matching methods can not contain a line feed and carriage return. [/ Size] [/ color] [/ quote]

Intelligent Recommendation

The realization principle of trim() method of the final class of String

The realization principle of trim() method of the final class of String The function of trim is to remove the spaces at both ends of the string. The source code is as follows: The principle is to take...

String class exercises: Method for simulating a TRIM function

Topic: Method for simulating a TRIM function Idea: 1, define two variables A variable is a corner that determines the string space from the beginning. Continuous ++. A variable begins to determine the...

String class explain

Immutability String The object is immutable (Immutable), that is, onceString Once a class instance is created, its value cannot be changed. Immutability here means that the reference can neither point...

TRIM in String

TRIM cut out the first specified character...

Explain the usage of MySQL string function TRIM(), fill function LPAD(), RPAD(), string length LENGTH() (3)

Article Directory 1. Delete space functions TRIM(), LTRIM(), RTRIM() 1、TRIM() 2、LTRIM() 3、RTRIM() 2. Fill function LPAD(), RPAD() 1、LPAD() 2、RPAD() Three, string length LENGTH(), CHAR_LENGTH() 1、LENGT...

More Recommendation

Explain the intern() method in the String class.

We use a classic example to understand true false false true The intern() method in String, this method should pay attention to the JDK 1.6 running, will get two false, and running in JDK 1.7, will ge...

Member variable value trim for all String types of the entity class

Using reflection, you can perform trim operations on the values ​​of all member variables of type String in the entity class. (1)trim Application scenario (2) Set the member variable whose value is an...

Java commonly used class String class to explain

String related class String class Java.lang, String represents an immutable sequence of characters. "xxxx" is an object of this class (1) The common constructor of the String class (the best...

Java learning summary - explain the String class in Java

Picking up the land - a deep interpretation of the String class First, the String class A large amount of text, characters, etc. that the program needs to store are represented by a string. Java handl...

javaSE basic learning day12-String class explain

Article directory Scanner's overview and method introduction Scanner gets small problems and solutions for data Overview of the String class String class construction method String class common interv...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top