String Manipulation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: String Manipulation

  1. #1
    Join Date
    May 2009
    Location
    .NET 3.5 SP1
    Posts
    3

    String Manipulation

    Okay, so I have set myself a project that might be a little in over my head for my first program, but I've gotten too far to quit now.

    I'm stuck trying to figure out the best (read: easiest) way to manipulate certain strings. The input is being read from a file line-by-line, and it's a capture from a text-based game. For example,

    Trios cuts adult red dragon for 73 damage!
    The adult red dragon breathes dragonfire on you for 174 damage!

    Now, I already have a file with a list of all the possible combinations of these messages, and for the ones above it looks like this:

    {source} cuts {target} for {dmg} damage!
    {source} breathes {misc} on {target} for {dmg} damage!

    So when I read in this file, I want to read a line, compare it to my preset messages, to determine A) what kind of message it is, B) who the {source} and {target} is, if any; and C) how much damage was done, if any.

    A little help would be appreciated. Thanks!

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: String Manipulation

    Just few ingredients: String.Format() method, StringBuilder class and regular expressions, but you have to cook it yourself, because I'm affraid there is no ready-made solution.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: String Manipulation

    Quote Originally Posted by Trios View Post
    {source} cuts {target} for {dmg} damage!
    {source} breathes {misc} on {target} for {dmg} damage!
    The format of this sentence is an easy approach like
    Code:
    string.message = String.Format("{0} cuts {1}  for {2} damage ! ", source,target,dmg);
    Reading and analizing your existing Datafile is another chapter and it is not possible to give any suggestions without knowing more about the game itself.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    May 2009
    Location
    .NET 3.5 SP1
    Posts
    3

    Re: String Manipulation

    I've already got the datafile read and in a dictionary, I just didn't know about String.Format (obviously new to programming!). I'll give that a whirl here in a few minutes and let you guys know how it works out for me.

    Thanks JonnyPoet for the example, that explains how to use String.Format a lot better than the MSDN article I was just looking at....

  5. #5
    Join Date
    May 2009
    Location
    .NET 3.5 SP1
    Posts
    3

    Re: String Manipulation

    Okay, this seems to be a bit more complicated than I originally imagined. I'm going to kind of abstractly describe what I want to do and maybe you wonderful folks could point me in the right direction.

    I have an XML file with all the messages that can be shown in this game. It looks like this:

    Code:
    <MESSAGES>
    	<PLAYER>You</PLAYER>
    	<PLAYER>Trios</PLAYER>
    	<PLAYER>Dolzac</PLAYER>
    	<PLAYER>LadyLuck</PLAYER>
    	<PLAYER>Kalgak</PLAYER>
    	<PLAYER>Amadeus</PLAYER>
    	<PLAYER>Jester</PLAYER>
    	<PLAYER>Lunatic</PLAYER>
    	<PLAYER>Caliss</PLAYER>
    	<PLAYER>Gardner</PLAYER>
    	<PLAYER>Bosh</PLAYER>
    	<PLAYER>Turbofoot</PLAYER>
    	<PLAYER>Tintin</PLAYER>
    	<PLAYER>Mitsy</PLAYER>
    	<PLAYER>Muddle</PLAYER>
    	<PLAYER>Walker</PLAYER>
    	<PLAYER>Mr</PLAYER>
    	<COMBAT>*Combat Engaged*</COMBAT>
    	<COMBAT>*Combat Off*</COMBAT>
    	<COMBAT>{source} moves to attack {target}</COMBAT>
    	<COMBAT>{source} moves to cast {misc} upon {target}.</COMBAT>
    	<COMBAT>{source} breaks off combat.</COMBAT>
    	<SPELL_HIT_ROOM>A whirling maelstrom assaults the room for {dmg} damage!</SPELL_HIT_ROOM>
    	<SPELL_HIT_ROOM>A whirling maelstrom assaults the room for {dmg} damage!</SPELL_HIT_ROOM>
    	<SPELL_HIT_ROOM>A massive blast of light sears the room for {dmg} damage!</SPELL_HIT_ROOM>
    	<SPELL_HIT_ROOM>A burning rain of blood pours down on the room for {dmg} damage!</SPELL_HIT_ROOM>
    	<SPELL_HIT_ROOM>A storm of freezing hail blows through the room!</SPELL_HIT_ROOM>
    ...and so on. I have these loaded into a Dictionary<string,List<string>> right now, with the key being the type of message (the part in <> above) and the list carrying each individual message.

    I need to:
    A) Compare these lines of text to a full line of an input file, to find a match with any of these messages, so the "search thru the dictionary" code must essentially ignore the parts in {}.
    B) Once I have a match, I need to save certain parts of the string, such as damage and source.

    I'm assuming I'm going to need to do more to the values in the dictionary (the list) to get them to compare properly with an input string, but I don't know how to go about it. I can't hard-code them, because the game changes (new spells, attacks, etc) and that's the reason for the XML file, so players can add to the list of possible messages in the case that their new attack doesn't show up as damage from my program.

    Is this making sense to anyone? I appreciate your help, truly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center