Author Topic: Replacing String  (Read 404 times)

Legacy_4Pop

  • Newbie
  • *
  • Posts: 28
  • Karma: +0/-0
Replacing String
« on: November 19, 2014, 12:48:37 pm »


               

I am looking for on player chat to replace each letter of a string he speaks to be converted to numbers. So, for example..


 


1234 = abcd.


 


I want to accomplish this using the SetPCChatMessage function, and everything. I know how to script everything else, just not taking what he says and converting it to numbers. The number of the letter in the alphabet is equal to its numeric value, so a = 1, b = 2, c = 3, etc. Thanks!


 


One more thing, I want to make this a custom function that can be included. Can that be done? Thanks!



               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Replacing String
« Reply #1 on: November 19, 2014, 09:14:24 pm »


               

nwn script did not give any of the standard ways of converting a letter to a number.   The simplest way that I think most people are using currently is something like: 


 




    string sLetterArray = "abcdefghijklmnopqrstuvwxyz";
    sLetter = GetStringLowerCase(sLetter);
    nLetterNumber = FindSubString ( sLetterArray,sLetter);


               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Replacing String
« Reply #2 on: November 19, 2014, 09:56:24 pm »


               

As a function to add to an include.  it would look something this untested function;


 




string ConvertStringToNumbers(string sOriginal)
{
    string sLetterArray = " abcdefghijklmnopqrstuvwxyz";
    int nLength = GetStringLength(sOriginal);
    string sNewString;
    string sLetter;
    int nLetterNumber;
    int x;
    for(x=0;x<nLength;x++)
    {


      sLetter=  GetSubString(sOriginal,x,1);
      nLetterNumber = FindSubString ( sLetterArray,GetStringLowerCase(sLetter));
      if ( nLetterNumber != -1) sNewString += IntToString(nLetterNumber) + ".";
      else  sNewString += sLetter;
    }
    return sNewString;
}