View Single Post
Old 02-23-2014, 02:17 AM   #113
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Here are two functions to remove or replace chars in a string (eel script, but should work in JS if ShowConsoleMsg(...) are removed):

Code:
// remove char(s) from string -> return modified str
function remove_char(string, char) local(c, pos)
(
  pos = 0;
  #temp_string = "";
  while (pos < strlen(string)) (
    (c = str_getchar(string,pos)) != char ? (
      #temp_string += sprintf(#, "%c", c);
    );
    pos += 1;
  );
  #temp_string;
);

// replace char(s) in string -> return modified str
// replaces "char" with "repl_char"
function replace_char(string, char, repl_char) local(c, pos)
(
  pos = 0;
  #temp_string = "";
  while (pos < strlen(string)) (
    (c = str_getchar(string,pos)) != char ? (
      #temp_string += sprintf(#, "%c", c);
    ) : (
    str_setchar(#temp_string, pos, repl_char);
    );
    pos += 1;
  );
  #temp_string;
);

#str1 = "0  ";
#str2 = "\"Major Triad 1\"";
#str3 = "     100300006000";

ShowConsoleMsg("Original: ");
ShowConsoleMsg("\n");
ShowConsoleMsg(#str1); // original string
ShowConsoleMsg("\n");
ShowConsoleMsg(#str2); // original string
ShowConsoleMsg("\n");
ShowConsoleMsg(#str3); // original string
ShowConsoleMsg("\n");
ShowConsoleMsg("\n");

#str1 = remove_char(#str1, '\t'); // remove tabs from #str1
#str2 = remove_char(#str2, '"'); // remove quotes from #str2
#str3 = remove_char(#str3, '\t'); // remove tabs from #str3

//#str2 = replace_char(#str2, ' ', '\t'); // replace "white spaces" with "\t"

ShowConsoleMsg("Modified: ");
ShowConsoleMsg("\n");
ShowConsoleMsg(#str1); // modified string (chars "removed")
ShowConsoleMsg("\n");
ShowConsoleMsg(#str2); // modified string (chars "removed")
ShowConsoleMsg("\n");
ShowConsoleMsg(#str3); // modified string (chars "removed")
ShowConsoleMsg("\n");

Last edited by spk77; 02-24-2014 at 03:07 AM. Reason: "<=" to "<"
spk77 is offline   Reply With Quote