String IndexOf function

A place to make suggestions about Thinking Worlds.

String IndexOf function

Postby C_M » Mon Apr 16, 2012 11:04 am

Hi,

I needed a string replace method in CScript. There wasn't one available, so I made my own. I'm providing it to you under a Creative Commons Attribution 3.0 Unported License.
The code is below, as well as attached (with usage info, copyright notice and proper indenting).

Code: Select all
function StringIndexOf(string source, string match) integer
{
   integer output := -1
   integer sourceLen := StringLength( source )
   integer matchLen := StringLength( match )
   integer i
   integer n := sourceLen - matchLen
   for i from 0 to n
   {
      string outputSubstring := GetSubstring( source, i, matchLen )
      integer x := CompareString( outputSubstring, match)
      if( x = 0 )
      {
         return i
      }
   }
   return -1
}


You might want to add it to StringFuncs.cscript in your next release.
Attachments
StringIndexOf.zip
String IndexOf method in Cscript
(588 Bytes) Downloaded 45 times
C_M
 
Posts: 52
Joined: Tue Nov 08, 2011 11:43 am

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest