I开发者_如何学Go'm trying to develop a script which compares a runtime generated string against one which is input by the user. Unfortunately, since user inputs his code using a textbox, I get ^M in the string input by user. Example, If I print these strings to file I get this:
User Input:
1^M
2^M
3
Output of script:
1
2
3
Obviously, when I try to compare these two strings, I get a false. What would be the most efficient way to sort this problem? Just in case you haven't noticed, all lines entered by user "donot" end with "^M". The last line in this user string has no ^M, while output of code has a "\n" at the end.
You should use str.splitlines()
to split the text coming from the textbox, instead of whatever it is you're using now. That method handles \r\n
properly.
mystring.rstrip('\r')
will return a new string with any ^M removed from the end of the string.
精彩评论