0

I am creating a program where I am converting bytes into a utf-16 string. However, sometimes the string will carry over because there are trailing 0's and my string will end up like: "This is my string x00\x00\x00". I want to trim the string when I reach the first \x00 or x00 which indicates trailing 0's. How do I do this in python?

My problem is not a duplicate of another question linked in comments because trim() doesn't fully work. If I have a string that is "This is my string x00\x00 hi there x00\x00" I want just "This is my string" whereas trim would return "This is my string hi there"

Avinash
  • 2,093
  • 4
  • 28
  • 41
user7683274
  • 19
  • 1
  • 4
  • welcome to SO, what have you tried? Can you show us the code ? – mugabits Mar 16 '17 at 01:15
  • Sounds like you want [strip](https://docs.python.org/2/library/string.html#string.rstrip) – Shadow Mar 16 '17 at 01:17
  • Possible duplicate of [Trimming a string in Python](http://stackoverflow.com/questions/761804/trimming-a-string-in-python) – Shadow Mar 16 '17 at 01:18
  • I tried getting the index of the string and then doing string manipulations from that to try and trim the string but it did not work. I would call index("x00") on the string then subtract the index from the length and call str[-diff] where diff is length-index. – user7683274 Mar 16 '17 at 01:18
  • Not sure if typo, but you _must_ escape null terminators in strings using a backslash or Python will try to index your string using the three character substring "x00". – JacaByte Mar 16 '17 at 01:50

2 Answers2

5

Use index('\x00') to get the index of the first null character and slice the string up to the index;

mystring = "This is my string\x00\x00\x00hi there\x00"
terminator = mystring.index('\x00')

print(mystring[:terminator])
# "This is my string"

You can also split() on the null characters;

print(mystring.split(sep='\x00', maxsplit=1)[0])
# "This is my string"
JacaByte
  • 325
  • 2
  • 8
0

Use strip() function can eliminate some characters that you don't want, for example:

a = 'This is my string \x00\x00\x00'
b = a.strip('\x00') # or you can use rstrip() to eliminate characters at the end of the string
print(b)

You will get This is my string as the output.

Hou Lu
  • 3,012
  • 2
  • 16
  • 23
  • The problem I had in one of my examples is that there could a string like "This is my string x00\x00\x00\ hi there \x00\x00" I would still want the output to be "This is my string". I have used strip() but this edge case has been throwing me off. – user7683274 Mar 16 '17 at 01:20
  • try this: `b = a[0:a.find('\x00')]` – Hou Lu Mar 16 '17 at 01:30