GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2020-08-16 06:58:27

redeyesmaster
Member
Registered: 2020-08-09
Posts: 43

Bundle of string functions

string_get:

Expand///string_get(stringID, prefix, suffix)
//
// @para   stringID     the string to look through
// @para   prefix       the begining of the string to find
// @para   suffix       the end of the string to find
//
// Returns : string

/// This function will return a string with the defined prefix and suffix
//  string_get("This is a test", "is", "a")
//  returned: "is is a"

//  useful for grabbing information in none standard formats

/// GMLscripts.com/license


var stringCopy = argument[0]
var prefix   = argument[1]
var suffix   = argument[2]

var prefixPos = string_pos(prefix, stringCopy)
if (prefixPos != 0){
    stringCopy = string_delete(stringCopy,1,prefixPos-1);
    var stringID   = string_copy(stringCopy,1,string_length(prefix));
    stringCopy = string_delete(stringCopy,1,string_length(prefix));
    var suffixPos = string_pos(suffix, stringCopy);
    if (suffixPos != 0){
        var suffixLength = string_length(suffix);
        stringID = (stringID + string_copy(stringCopy,1,(suffixPos+suffixLength)-1));
        return stringID
    }else{
        return undefined;
    }
}else{
    return undefined;
}

string_remove:

Expand///string_remove(stringID,subString)

// stringID - source string
// substring - the string to remove
//
// return: string or undefined

/// GMLscripts.com/license

var stringCopy = argument[0];
var sPos    = string_pos(argument[1],stringCopy);
if (sPos != -1){
  var sLength = string_length(argument[1])
  stringCopy = string_delete(stringCopy,sPos,sLength);
  return stringCopy;
}else{
  return undefined
}

string_remove_before:

Expand///string_remove_before(stringID,subString)

// stringID - source string
// substring - the string to remove everything before

// returns string or undefined

/// GMLscripts.com/license

var stringCopy = argument[0];
var sPos    = string_pos(argument[1],stringCopy) - 1;
if (sPos != -1){
  stringCopy = string_delete(stringCopy,1,sPos);
  return stringCopy;
}else{
  return undefined
}

string_remove_after:

Expand///string_remove_after(stringID,subString)

// stringID - source string
// substring - the string to remove everything after

// returns string or undefined

/// GMLscripts.com/license

var stringCopy = argument[0];
var sPos = string_pos(argument[1],stringCopy);
if (sPos != -1){
  var sLength = string_length(argument[1])
  stringCopy = string_delete(stringCopy,sPos+sLength,string_length(stringCopy));
  return stringCopy;
}else{
return undefined
}

string_pos_nonCaseSensitive:

Expand///string_pos_nonCaseSensitive(substring,string)

// substring - the string to find the possition of
// stringID - source string

// returns string or -1

// Note: This string function is different from my others because it's based off a real string function,
//   This function will also grab any spaces, underscores, dashed, etc.
//   This script was made to grab the location of a game's title on a web scraper, some sites had different formats for titles in the html

/// GMLscripts.com/license

var stringcopy    = argument[1]
var substringcopy = argument[0]

var stringcopy    = string_lower(stringcopy)
var stringcopy2   = stringcopy
var substringcopy = string_lower(substringcopy)

//This is a special instance to remove the space at the end of a title incase there was a mistake, if you dont the script will think there will be another word causing a crash
if (string_pos(" ", substringcopy) = string_length(substringcopy))
    { substringcopy = string_delete(substringcopy, string_length(substringcopy), 1)  }


var pos = string_pos(substringcopy, stringcopy)

if (pos == 0)
{
  //just incase there are double spaces this is where we remove them
  del = string_pos("  ", substringcopy)
  while (del != 0){
    substringcopy = string_replace(substringcopy, "  ", " ")
    del = string_pos("  ", substringcopy)
  }
    
  //loop through the string dividing the words apart
  var _i = 0
  var word_array
  substring_divide = substringcopy
  //if the first charactor is " " then remove it
  if (string_pos(" ",substring_divide) = 1) {substring_divide = string_remove(substring_divide," ")}
  var del = string_pos(" ", substring_divide)
  
  while (del != 0){
    word_array[_i] = string_copy(substring_divide, 0, del-1)
    substring_divide = string_delete(substring_divide, string_pos(word_array[_i],substring_divide), string_length(word_array[_i])+1)
        
    del = string_pos(" ", substring_divide)
    _i++
  }
  //then copy the final word into the array.
  if (substring_divide != ""){  //this extra check is to make sure the substring didnt end in a space, just prevents a crash
    word_array[_i] = substring_divide
  }
  ///Check to make sure every word is even on the page
  var _i = 0
  words_found = true
  while (_i < array_length_1d(word_array)){
    test_pos = string_pos(word_array[_i], stringcopy)
    if (test_pos = 0){
      var words_found = false
      break;
    }else{
      _i++
    }
  }
    
    
  if (words_found = true){
    var valid = false
    
    pos = 0
    var first_word_pos = string_pos(word_array[0], stringcopy)
    while (first_word_pos != 0){
      real_string = string_copy(stringcopy, first_word_pos, (string_length(stringcopy)-first_word_pos)+1)
      stringcopy = string_delete(stringcopy, 1, first_word_pos+string_length(word_array[0]))
      var i = 1
      next_word_pos = string_pos(word_array[i], stringcopy)
      stringcopycopy = stringcopy
      
      dist = string_length(word_array[0])
     
      while (next_word_pos !=0){
        //if we've reached the last word, break
        if (i = array_length_1d(word_array)-1){
          if (next_word_pos <= 6){
            //found all words with in a reasonable distance of one another
            var valid = true
            dist = dist + (string_pos(word_array[i], stringcopycopy)+string_length(word_array[i]))
            stringcopycopy = string_delete(stringcopycopy, 1, next_word_pos+string_length(word_array[i]))
            
            break;
          } else { break; }
        }
                
        ///copy everything from the end of the first word onwards
        dist = dist + (string_pos(word_array[i], stringcopycopy)+string_length(word_array[i]))
        stringcopycopy = string_delete(stringcopycopy, 1, next_word_pos+string_length(word_array[i]))
                
        if (next_word_pos <= 6){  //if word is with in a reasonable distance
          next_word_pos = string_pos(word_array[i+1], stringcopy)
        }else {  break; }   ///if the word is not with in a reasonable distance; break, and find the next instance of the first word.
        i++
        next_word_pos = string_pos(word_array[i], stringcopycopy)
    }
    if (valid = true){break;} //if we found all the words break
    first_word_pos = string_pos(word_array[0], stringcopy) 
  }
  
  //when all the words are found, copy everything from first_word_pos to the distance to the last word+the last words length
  if (valid = true){
    var true_search = string_copy(real_string, 1, dist)
    pos = string_pos(true_search, string_lower(argument[1]))
    }
  }
}

return pos;

These scripts are all about two years old at this point, so the code may seem a little messy. Most of the scripts are pretty simple to replicate although if someone is looking for more string functions these are a great addition especially for dealing with web scraping. Or for non standardized text formatting where the string you're looking for could be before or after a specified string, or for the worse case scenario when you need to find a string and it could have spaces, dashes, or underscores.

Offline

#2 2020-08-17 16:03:57

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Bundle of string functions

Thanks! These look useful. I may have some questions later.


Abusing forum power since 1986.

Offline

#3 2020-08-18 01:51:44

redeyesmaster
Member
Registered: 2020-08-09
Posts: 43

Re: Bundle of string functions

Yeah feel free to get in contact with me when ever. You have my discord if you wish to get replies a bit faster. We could discuss there, and post important details here for documentation's sake.

Offline

Board footer

Powered by FluxBB