I have encountered a strange problem today. It is necessary to share with you about
Trim
.
Trim
There are many application scenarios in our project, so it is very necessary to share with you. We know
Trim
Filter by default
* "(ASCII 32 (0x20), an ordinary space.
* "/T" (ASCII 9 (0x09), a tab.
* "/N" (ASCII 10 (0x0a), a new line (line feed ).
* "/R" (ASCII 13 (0x0d), a carriage
Return.
* "/0" (ASCII 0 (0x00), The NUL-byte.
* "/X0b" (ASCII 11 (0x0b), a vertical tab.
You can also specify filter fields for these characters. I have a requirement to filter out
URL
In
Http ://
So, I use
$ Stie = http://tbtest101.tmail.taobao.net ";
$ Site = trim ($ site, "http ://");
But the result is:
Btest101.tmail. Taobao. ne
Note:
URL
At the end
. Net
Change
. Ne
, Causing the entire query to fail. Then I try to use
$ Stie = "http://tbtest101.tmail.taobao.cn
";
$ Stie = http://tbtest101.tmail.taobao.com ";
$ Stie = http://tbtest101.tmail.taobao.me ";
These
URL
Test and find that all of them can output the correct results. Is it the last
T
The entire filter is affected, and then I change the test condition
$ Site = "test ";
If not, the output result is
Tes
The last one
T
. This reminds me
Trim
Default Filter
/T
, Will it
R, n
Will the ending character be filtered out.
$ Site = "test N ";
$ Site = "test T"
The output result is
;
Test
N
Test
R
Found
Trim
There is no query statement
"//"
,
Trim
Filter errors. Then I try to use
Ltrim
Test
$ Stie = http://tbtest101.tmail.taobao.net ";
$ Site = ltrim ($ site, "http ://");
The result is
Btest101.tmail.taobao.net
Found
Ltrim
The result is correct.
Ltrim
And
Trim
The filtering mechanism is different. In fact, you can view the document clearly,
Ltrim
It only starts from the left side of the string.
Trim
Character. It seems that the problem has been solved. Use
Ltrim
Replace
Trim
That's all. For the specific reason, you can consider it
Trim
One
Bug
.
However, there is a huge
Bug
. In the preceding example
$ Stie = http://tbtest101.tmail.taobao.net ";
After filtering,
$ Site
The object is
Btest101.tmail.taobao.net
In fact, you will find that
URL
In
Tbtest101
After filtering
Btest101
T
Also filtered, use
Trim
And
Ltrim
The results are the same. You can only take a closer look at the document,
Charlist
Optionally, the stripped characters can also be specified
UsingCharlist
Parameter. Simply list all characters that you want
To be stripped...
You can specify a range of characters.
Trim
The second parameter is
String
Type, variable name
Charlist
, Take a closer look at the description of the document.
Trim
It is used to filter characters rather than strings. When we want to filter a part of a string, if we use the string to be filtered as the filter condition, this part of the string will certainly be dropped, but it will also cause other weird behavior, these weird behavior Pairs
Trim
It is completely legal. For example
$ Test = "This
Is trim test, I find one bug, but is not a error ";
If we want to filter
Error
, Use
Trim
, May be used like this
Trim ($ test, "error ");
The output result is
This is
Trim test, I find one bug, but is not
Haha, the output is correct. Actually, you know
Trim
You can write
Trim ($ test, "eror ");
Of course, you can do the same.
Trim ($ test, "EOR ");
Same implementation and
Trim ($ test, "error ");
Same effect. Here, we should have a general understanding
Trim
.
Trim
When filtering characters, it starts with matching the first character until the first character does not meet the conditions. Let me explain this sentence in my first example!
$ Stie =
Http://tbtest101.tmail.taobao.com ";
My filter is
$ Site =
Trim ($ site, "http ://");
The filtered result is
Btest101.tmail. Taobao. ne
Tbtest101
, Filter
Btest101
The reason is
Http ://
What follows is
T
Of course, if
H
And
P
This character is also filtered out.
So
. Net
Change
. Ne
Why?
Trim
In addition to the first letter matching rule mentioned above, there is also a rule, that is, tail matching filtering, if
. Net
Of
T
Change
H
,
P
The same character will be filtered out, which is the tail filter. It should be said that this behavior is not what we want, so
Trim
Divided
Ltrim, rtrim
,
Ltrim
The corresponding policy is the first rule I mentioned, which matches the first letter of a field.
Rtrim
The corresponding policy is the second rule I mentioned, which matches the end of the field.
While
Trim
The policy is exactly
Ltrim + rtrim
And. About
Trim
So far.