Python3 the head / tail deleted proper operation of the sub-string

I. Description

Found python string variable comes from sometime strip () method, in addition to the head and tail of the string can be deleted space can also be used to delete the beginning and end of strings feel good with. It has been so used, it has also found nothing wrong.

Today bug when used in the repair of a strip () method but the results look bug and has not been eliminated as expected, one did not suspect strip () Delete substring What is the problem, the second is a long process code, the third is dependent on the lower libraries and less reliable, troubleshoot most of the day is the last to know strip () due to the use of a wrong interpretation, regarded suffered a major loss.

 

Second, the erroneous deletion operation example substring

2.1 Wrong strip () operation

Suppose we have one of the following string, we) remove its tail "str" ​​substring no problem rstrip (, but the problem by deleting the tail rstrip () "_str" appears substring Shique.

# Sample string 
test_str = " this_is_a_test_str " 

# expect delete trailing "str", results in line with expectations 
# is expected to be "this_is_a_test_", the result is "this_is_a_test_" 
test_str.rstrip ( " str " ) 

# expect delete trailing "_str" results and expectations do not match. 
# Is expected to be "this_is_a_test", is actually "this_is_a_te" 
test_str.rstrip ( " _str " )

 

2.2 strip () Rationale

The reason why this happens is not in line with expectations upside will appear, because the strip () is not to remove "a given string", but to remove the given set of characters until it encounters a character not in the set Until characters.

In test_str.rstrip ( "str"), the character set is "s", "t", "r" three characters, character strings by The rstrip () Find character from right to left indicates delete, delete when finished. " str "after encountering a" _ ", and" _ "is not in the character set is deleted stopped, so the result is" this_is_a_test_ "; and to delete" str "string consistent with the results, but this is just a coincidence.

When test_str.rstrip ( "_ str"), the character set is "_", "s", "t", "r" four characters, strings, according to rstrip () indicates the start looking from right to left to delete a character, when you are finished removing "_str" next "t" and "s" are still concentrated in the character is still so removed, so the result is "this_is_a_te", rather than "this_is_a_test".

 

2.3 A typical error string interception] [skippable

# Sample string 
test_str = " this_is_a_test_str " 

# following desirable to be able str this truncated tail and head 
# of forgetting to give "is_a_test", the actual result is "a_test_str" 
test_str.lstrip ( " this_ " ) [: test_str .rindex ( " _ " )]

Why left "is_" has also been deleted in the previous section already said clearly, why the right of the "_str" has not been removed yet, because lstrip () does not modify the original test_str but returns a new string while test_str.rindex ( "_") to locate the original is still "this_is_a_test_str" of "_" is not a new position to return to "a_test_str" of "_" position.

 

Third, the right to delete substring operation

Some places say you can use the built-in string replace () method, but replace () will replace all matches that are very rough, more often we want to delete the string may be explicitly location.

3.1 Use len ()

# Sample string 
test_str = " this_is_a_test_str " 

# expect "this_is_a_test", the actual result is "this_is_a_test" 
test_str [: - len ( " _str " )]

 

3.2 re.sub ()

Import Re 

# sample string 
test_str = " this_is_a_test_str " 

# expect "this_is_a_test", the actual result is "this_is_a_test" 
the re.sub ( " _str $ " , "" , test_str)

 

reference:

https://stackoverflow.com/a/1038845

https://www.geeksforgeeks.org/python-remove-the-given-substring-from-end-of-string/

Guess you like

Origin www.cnblogs.com/lsdb/p/12519837.html