How to refer to the space character? Null? ack

Started by JoelCBarker, Sun 22/06/2014 07:49:40

Previous topic - Next topic

JoelCBarker

Can't find the solution around the place

I have a string which contains multiple words separated by spaces, and I want to format it into a separate string for each word. It could be this sentence!

Code: ags

String user_text_input;    //    this is the raw string input from a text box prompt, "It could be this sentence!"

String first_word;
String second_word;    //    and so on...

function Input_Formatting() {
    if (user_text_input.Chars[1] == ?)    //    and I'm stumped. I want '?' to be the symbol for a space. How do I refer to a space here?


    If that isn't clear, I'm trying to go character by character through the input string to find a space, then I can store the preceding characters in my first_word string. I bet I can use variables to keep track of which Chars[] I've checked and need to ignore to keep moving through the string to find the next space and save the next word and on and on, through a long if (user_text_input.Chars[]==?) gauntlet to cover all of the characters allowed in my simple text box.

    This way I can know the order of the words, parse them for a word group and then have some clue what the user meant in a more thorough way.

    How do I look for a space in a string? Are there spaces in strings? Are there strings in space? :~(

-Joel

Babar

The ultimate Professional Amateur

Now, with his very own game: Alien Time Zone

Joe

As Babar says you can use the enum notation. But you can also use classical notation like: ' ' (with quotes) if your want to refer to question mark you can use '?' (Always with qoutes). NOTE simple quotes are for simple characters and double quotes are for strings.
Copinstar © Oficial Site

JSH

I'd suggest looking up the parser in the manual first, it may do exactly what you want without any manual string formatting. If it's not enough for your purposes, I'd suggest having a look at the string formatting functions in the manual.

BreakIntoWords() below contains some adapted code from Kathy Rain using the aforementioned string functions to break a string into an array of substrings. DisplayWords() then shows how you can iterate through the words and display the words on the screen one by one:

Code: ags

#define NOT_FOUND -1
#define MAX_NUM_WORDS 16
  
String Words[MAX_NUM_WORDS];
int nWords = 0;

function BreakIntoWords(String _Source)
{
  bool bContinue = true;
  nWords = 0;
  int iWS;
  
  while(bContinue)
  {
    iWS = _Source.IndexOf(" ");
    
    if(iWS == NOT_FOUND)
    {
      Words[nWords] = _Source; // No whitespaces found, treat the remainder of the string as the final word
      bContinue = false;
    }
    else
    {
      Words[nWords] = _Source.Truncate(iWS);
      _Source = _Source.Substring(iWS + 1, _Source.Length);
    }
    
    nWords++;
  }
}

function DisplayWords()
{
  int i = 0;
  while(i < nWords)
  {
    Display("\"%s\"", Words[i]);
    i++;
  }
}

JoelCBarker

#4
Excellent! Thanks guys.

eKeySpace is only useful at the time it gets on-key-pressed right? I know I could save a word once space was pressed but I want the user to be able to edit the scentence before submitting with enter

That was my problem, "" vs ''

Kathy Rain looks amazing btw!
Yea I couldn't wrap my brain around a solution with the parser, but this looks fine, dandy even


JoelCBarker

#5
Thanks for helping with that problem!

Now that I can separate my strings into individual words, I want to write them to .rtf log type files, and I have no problem doing that.

However, I want to jump into one of those .rtf files, or .dat or what have you, go to a specific line in that file and change a character without overwriting the rest of the characters in the file.

Am I limited only to eFileWrite and eFileAppend? I know about WriteRawString and WriteRawCharacter, but I can't find a method to change a specific character without overwriting the rest of the file contents or simply appending.

I want to store variables and strings in a .dat with the intent to read and edit them accross seperate game sessions. That way, my pathetic AI can store memory outside of its code which will allow it to "learn" more variables than it was originally given. Does that make sense?

I'm trying to store things in .dat files to be read later and I want the ability to edit them in part rather than rewriting the entire file.

The structure of the .dat would be something like a seperate variable on each newline, where the program could be directed to the line in a file and either read or change the character or number on that line.


sigh


Doh nevermind, I found out how read it, replace char at, and rewrite it. Thanks! Keeping the sigh though

SMF spam blocked by CleanTalk