How to remove first or last few characters from a string?
Question:
I have only ever written a few lines and don´t really understand what I´m doing. So could any one please explain to me why this doesn´t work for "end = 0" and how could I make it work?
Thank you kindly 🙂
start = rs.GetInteger
end = rs.GetInteger
end = -end
objectIDs = rs.GetObjects("Pick some object")
for singleID in objectIDs:
name = rs.ObjectName(singleID)
name = name[start:end]
rs.ObjectName(singleID,name)
… so if the object name is 123456789 and I input 5 and 0 I get 6789.
If I input 0 and 4 I get 12345.
For 2 and 3 I get 3456, etc.
edit:
OK, thank’s to you I think I understand why it doesn´t work and I think I fixed it thus:
start = rs.GetInteger
if start == 0: start = None
end = rs.GetInteger
end = -end
if end == 0: end = None
objectIDs = rs.GetObjects("Pick some object")
for singleID in objectIDs:
name = rs.ObjectName(singleID)
name = name[start:end]
rs.ObjectName(singleID,name)
Answers:
-0 is 0. So, when you have
123456789
as input and 5 as start, 0 as end, then it will be the very first. So, you sliced the first 5 characters.
With 0 and 4, -4 means that except the last 4 four characters, so you get 12345.
I have only ever written a few lines and don´t really understand what I´m doing. So could any one please explain to me why this doesn´t work for "end = 0" and how could I make it work?
Thank you kindly 🙂
start = rs.GetInteger
end = rs.GetInteger
end = -end
objectIDs = rs.GetObjects("Pick some object")
for singleID in objectIDs:
name = rs.ObjectName(singleID)
name = name[start:end]
rs.ObjectName(singleID,name)
… so if the object name is 123456789 and I input 5 and 0 I get 6789.
If I input 0 and 4 I get 12345.
For 2 and 3 I get 3456, etc.
edit:
OK, thank’s to you I think I understand why it doesn´t work and I think I fixed it thus:
start = rs.GetInteger
if start == 0: start = None
end = rs.GetInteger
end = -end
if end == 0: end = None
objectIDs = rs.GetObjects("Pick some object")
for singleID in objectIDs:
name = rs.ObjectName(singleID)
name = name[start:end]
rs.ObjectName(singleID,name)
-0 is 0. So, when you have
123456789
as input and 5 as start, 0 as end, then it will be the very first. So, you sliced the first 5 characters.
With 0 and 4, -4 means that except the last 4 four characters, so you get 12345.