- Home /
string.TrimEnd() is ignoring a character.
First here is my code, C#(sharp);
//for reference the object name is Track_Turn;
public string assetPrefix = "Model_";
public int trimStartIndex = 0;
public int trimLength = 6;
string assetName;
char[] gameobjectPrefix = gameObject.name.ToCharArray(trimStartIndex, trimLength);
assetName = assetPrefix + gameObject.name.TrimStart(gameobjectPrefix);
Now I would assume that the output I should get is Model_Turn,
but instead I get Model_urn.
I tried changing the trimLength to 5 and I got Model__Turn.
So I would like to know why its not outputting
Model_Turn when the character length of Model_ and Track_
is 6. I also have other objects like Track_Straight and
Track_90Turn, they all turn out fine with trimLength 6.
EDIT: wow that's werid I had to set the format to code because the underscores were causing format problems.
Answer by ScroodgeM · Aug 17, 2012 at 07:03 AM
string.TrimStart method removes all symbols from begin of string that you give it as array.
when you give 'em 6 symbols 'Track_', it takes full name 'Track_Turn' and removes all symbols that exists in first array. so, it removes 'Track_T', cause next symbol 'u' is not exists. output is 'urn'
when you give 'em 5 symbols 'Track' - it stops on '_' symbol and you get output as '_Turn'
use
string.Replace("Track_", "Model_");instead
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Error help ( error CS0023 The `!’ operator cannot be applied to operand of type `string’ ) ??? 1 Answer
Distribute terrain in zones 3 Answers
CSharp Classes = Scripts? Difference between private string and normal string? 1 Answer
Generate Json from Class Type List 1 Answer