Author Topic: Tutorial on Str commands (strReplace)  (Read 2146 times)

Can someone make one, please.
Im always so confused about them.


String commands are useful for manipulation of string variables, which are variables who's values include text. Torque by default makes all variables strings, so you don't have to worry about them not working for a variable.

The string functions and their use are as follows:

Code: [Select]
strChr(string, character)
    - This function takes an input string (string) and checks to see if a character (character) is contained in that string. If so, it returns the string, otherwise, it returns nothing.

strCmp(stringone, stringtwo) / striCmp
    - This function compares two strings, checking to see if the input string (stringone) starts with or is equal to the second string (stringtwo). If it is the same string, it returns 0. If it starts with that, it returns 1. If it doesn't start with that, it returns -1. striCmp is case insensitive.

strPos(stringone, stringtwo) / striPos
    - This function checks to see if stringtwo is located in stringone, and returns its position in the word if it is. For example, if you were checking to see if "triple" was part of my name (TripleNickels) and you used strPos, you would use strPos("TripleNickels","triple"). This would return false, since strPos is case sensitive and striPos is not.

strLen(string)
    - This function gives you the length of a string in characters. "a" is one one, "abc" is three long, etc.

strLwr(string)
    - This string makes whatever you input all lowercase.

strReplace(stringone, stringtwo, stringthree)
    - This string replaces any instances of stringtwo in stringone with stringthree. For example, if you wanted to turn the word "stuff" into ****, you could use strReplace("I feel like a pile of stuff","stuff","****") and it would replace stuff with ****.

strStr(string)
    - Does the same as strPos.

strUpr(string)
    - Makes your input all uppercase

getSubStr(string, start, count)
    - Gets all characters from the start point until count is up. If I wanted to get the first 5 characters of "How is your day?" I could run getSubStr("How is your day?",0,5)

getCharCount(string, char)
    - Counts all the characters in the string, for example getCharCount("Crazy Cathy eats Cake at Church","C") returns c. Case sensitive.

stripChars(stringone, stringtwo)
    - Removes stringtwo from stringone, for example stripChars("I hate catfish.","cat") returns "I hate fish."

lTrim(string)
    - Removes spaces from the beginning of the string. Changes " hey" to "hey".

rTrim(string)
    - Removes spaces from the end of the string. Changes "hey " to "hey"

trim(string)
    - Does both lTrim and rTrim. Removes spaces from both side, making " hey " "hey" or doing lTrim or rTrims job.

These are all the string commands listed from console, if you feel I missed any I will add them to the list.
« Last Edit: April 21, 2011, 10:12:21 PM by TripleNickels »

I'm writing a tutorial right now, just hold on a bit and it'll be done.

I'm writing a tutorial right now, just hold on a bit and it'll be done.

Post in topic and maybe sticky?

i was almost done typing it then my browser was closed.

D:

So what do you want to know specifically? Basic string and word manipulation?

EDIT:
You'll probably end up reading almost the exact same thing either way, so here's a reference.

http://docs.garagegames.com/tge/official/content/documentation/Reference/Console%20Functions/TorqueScript_Console_Functions_3.html
« Last Edit: April 22, 2011, 01:18:50 AM by otto-san »

Did no one notice that I spent 15 minutes explaining every string function? :(

You've got a pretty good list there, but this is best reference:

http://docs.garagegames.com/tge/official/content/documentation/Reference/Console%20Functions/TorqueScript_Console_Functions_3.html

It shows all of the string functions, including a few you missed, along with word and field manipulation.

EDIT: oops, otto already posted it.

Did no one notice that I spent 15 minutes explaining every string function? :(
Wow! 15 whole minutes!
I hope you didn't miss anything important!

Sarcasm aside, the link is the better reference, it's what I always use.

Wow! 15 whole minutes!
I hope you didn't miss anything important!

Sarcasm aside, the link is the better reference, it's what I always use.
15 minutes is more than I've ever spent making a reply to a coding help topic.

15 minutes is more than I've ever spent making a reply to a coding help topic.
i spent about an hour typing out a tutorial then it was all ruined by someone closing it


and the tutorial was basically the link posted.

i'm sure your help is appreciated, nobody is saying it wasn't any help at all.

So would:
Code: [Select]
function isNumber(%str)
{
for(%i=0;%i<strLen(%str);%i++){
if(strCmp(getSubStr(%str,0,%i),"1234567890"))
return 1;
else
return 0;
}}}
work? I wrote that on iPad. Help?

The best way to check if something is a number is like this:
Code: [Select]
function isInt(%string)
{
return %string $= mFloatLength(%string,0);
}
But if you still want to do it your way, you'd do something like this:
Code: [Select]
function isNumber(%str)
{
%len = strLen(%str);
for(%i=0;%i<%len;%i++)
{
%c = getSubStr(%str,%i,1);
if(strPos("123456789",%c) == -1)
return 0;
}
return 1;
}
NOTE: Your way will not work for decimal numbers.

So mine is error free/will work? Hot!

So mine is error free/will work? Hot!
It will not work for decimals.
EDIT: And no, the code I posted will. Yours has errors in it.