6

Ok, I have this string Player.Character with this in it "Average Man{[Attributes (Mind 10) (Body 10) (Soul 10)]}".

And I have this do-loop set up so that it should be going through this string 1 character at a time and seeing if its this "[" while adding each character it checks to another string ContainerName. The thing is ContainerName only has this in it "[" and I want it should have "Average Man{".

If some one could help me understand why it that this is happening and possibly a solution that my amature mind could handle I would be most gracious.

O ya, here be my code.

int count = -1;

string ContainerName = "";

//Finds Start of container
do
{
    count = count + 1;
    ContainerName = ContainerName + Player.Character[count].ToString();
} while (Player.Character[count].ToString() != "[" && 
         Player.Character.Length - 1 > count);

textBox1.Text = ContainerName;
7
  • 7
    Honestly, the best answer to this question is to refactor your code - Player.Character should be a class of its own, not a string that you have to parse. Commented Dec 20, 2012 at 15:35
  • Wouldn't a simple Split() be easier? Commented Dec 20, 2012 at 15:38
  • I should have mentioned that Player is class and Character is string stored in it. Commented Dec 20, 2012 at 15:39
  • 1
    "I have this string "Average Man{[Attributes (Mind 10) (Body 10) (Soul 10)]}"." - is this format self-defined? Can't you look into a standard format that has built-in parsers in the .NET Framework, like XML or JSON? Commented Dec 20, 2012 at 15:41
  • 1
    Nothing prevents you from having once class as a member stored in another. Character should be a class. End of story. At very least, stick with json. Commented Dec 20, 2012 at 15:41

2 Answers 2

1

Your code works fine (I just tested it). ContainerName will have value "Average Man{[". Player.Charecter probably doesn't have the right value. I used Player.Charecter = "Average Man{[Attributes (Mind 10) (Body 10) (Soul 10)]}"

A more elegant solution would be

TextBox1.Text = Player.Character.Substring(0, Player.Character.IndexOf('['))
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answering the actual question. I found that it works also.
Indeed it does, lol. After u said this I noticed I had a line of code that I had overlooked that was changing my string. Oops. That only took my an hour to notice.
Your solution does look better but I can't say I know enough to understand what it does.
It does three things: -Finds the position of "[" in the string (starting from 0, so in the example it would be 13) -Builds a string of every character between the two positions in that string (in the example everything from 0, the start of the string, to 13, the position of "["; which would give us "Average Man {[") -Makes the text inside Texbox1 the value of the string that was built up.
This will cause an ArgumentOutOfRangeException if '[' is not present in the string..
|
1

I'm assuming Character is a string property of Player, and you want the string up to the first [ or the entire string if it doesn't exist.

StringBuilder sb = new StringBuilder();
foreach (char c in Player.Character)
{
    if (c == '[')
        break;
    sb.Append(c);
}
textBox1.Text = sb.ToString();

or

var i = Player.Character.IndexOf('[');
textBox1.Text = i >= 0 ? Player.Character.Substring(0, i) : Player.Character;

2 Comments

The second example doesn't work. .Substring(int) returns everything after the index therefore "Average Man{[Attributes".Substring(12) == "[Attributes", where they want "Average Man{".
Oops, indeed. Changed it to Substring(0, i)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.