tags: jobs
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
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...
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...
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 cut out the first specified character...
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...
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...
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...
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...
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...
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...