ScriptSpot

Forum topic · General Scripting

Help with the trimleft/trimright function

By 3dwannab · 2013-09-13

Description

 

I've taken my favourite script and tried to learn from it. It's probably is a bit complicated for a beginner but I really wanted to add a function to trim any leading and trailing whitespace characters and also add an option for Title case and Sentence case. I read the manual and all I can see is:

 ToLower + ToUpper

Is there another way to achieve Title case and Sentence case?

Any code that I've modified has --------------#### 3dwannab code ###-------------- beside it.

The area that needs attention is:

--------------#### 3dwannab code ###--------------

( if trailing_leading = false then

for i in selection do i.name = trimleft trailing_leading

--------------#### 3dwannab code ###--------------

 

I'm completely suck at the minute and I wanted to chat with someone on here to help me figure out what I need to do. I've managed to get the presets working as they should. It's just the function that needs a look at.

Thanks

 

Comments (23)

3dwannab · 2017-06-03

I know this is an old thread but you're right about dotnet:

/* FN to change text with arguments. Valid arguements are :-
(fn_changeCaseString "ToUpper") "string here"
(fn_changeCaseString "ToTitleCase") "string here"
(fn_changeCaseString "ToLower") "string here"
*/
fn fn_changeCaseString caseType =
(
	execute("(DotNetClass \"System.Globalization.CultureInfo\").CurrentCulture.TextInfo." + caseType)
)

-- FNs with arguements & Results:
(fn_changeCaseString "ToUpper") "test_text is HeRe. Respects ABV. words --> BRB UFC"
-- "TEST_TEXT IS HERE. RESPECTS ABV. WORDS --> BRB UFC"

(fn_changeCaseString "ToTitleCase") "test_text is HeRe. Respects ABV. words --> BRB UFC"
-- "Test_Text Is Here. Respects ABV. Words --> BRB UFC"

(fn_changeCaseString "ToLower") "test_text is HeRe. Respects ABV. words --> BRB UFC"
-- "test_text is here. respects abv. words --> brb ufc"

*CORRECT FILENAME*

barigazy · 2013-09-14


fileP = "C:\temp\tempFolder\newFolder\tempFile.xxx"
fn correctString str =
(
str = substituteString str "\n" "\\n"
str = substituteString str "\t" "\\t"
)
correctString fileP
--> "C:\temp\tempFolder\newFolder\tempFile.xxx"

*CLEAN DUBLE PUNKCTION MARKS*

barigazy · 2013-09-14


fn cleanDuble str signArr =
(
local newstr = str[str.count]
for i in str.count-1 to 1 by -1 where not ((findItem signArr str[i]) != 0 and str[i] == str[i+1]) do (newstr = str[i]+newstr)
return newstr
)
str = "1,,,,,,,,0,..20,3-........-0,4,,,,,,,,,,,0,..,,-5----0,,,,,,,,,,,--"
cleanDuble str #(",",".","-")
-->"1,0,.20,3-.-0,4,0,.,-5-0,-"

*FIND AND REPLACE ALL IDENTICLE WORD*

barigazy · 2013-09-14


str = "were not officially supported for use in your own scripts, not for now"
fn FindAndReplaceAll str fnd rpl =
(
local addpos, fndcount = fnd.count
while ((addpos = findString str fnd) != undefined) do ( str = replace str addpos fndCount rpl )
)
FindAndReplaceAll str "not" "will"
--> "were will officially supported for use in your own scripts, not for now"

*FIND AND REPLACE FIRST IDENTICLE WORD*

barigazy · 2013-09-14


str = "were not officially supported for use in your own scripts, not for now"
fn FindAndReplace str fnd rpl =
(
addpos = findString str fnd
if (addpos != undefined) do (replace str addpos fnd.count rpl)
)
FindAndReplace str "not" "will"
--> "were will officially supported for use in your own scripts, not for now"

*FILTER NAME NUMBERS*

barigazy · 2013-09-14


fn filterSufix str =
(
local theNumbers = "1234567890"
local str = trimLeft (trimRight str)
if str.count != 0 do for i = str.count to 1 by -1 do
(
if findString theNumbers str[i] == undefined do (str = replace str i 1 "")
) ; str
)
filterSufix " $VRayCamera001"
-->"001"

*CLEAN STRING*

barigazy · 2013-09-14


fn cleanString st =
(
local theChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 _-."
st = trimLeft (trimRight st) -- this wil remove white spaces
if st.count != 0 do for i = st.count to 1 by -1 do
(
if findString theChars st[i] == undefined do (st = replace st i 1 "")
)
st
)
str = " >>CA%ME$RA==0~01<<<"
cleanString str
-->"CAMERA001"

*CAPITALIZE ONLY FIRST LETTERS*

barigazy · 2013-09-14


fn capitalizeFirstLetter str =
(
if str != "" do
(
fn upperFirst s = ((toUpper s[1]) + (substring s 2 s.count))
local strArr = filterString str " ", resultStr = ""
for s = 1 to strArr.count do
(
if strArr.count == 1 then (resultStr = upperFirst strArr[1]) else
(
if s < strArr.count then resultStr += upperFirst strArr[s]+" "
else resultStr += upperFirst strArr[s]
)
)
resultStr
)
)
msg = "max have very cool string methods but .net is even better"
capitalizeFirstLetter msg
-->"Max Have Very Cool String Methods But .net Is Even Better"

barigazy · 2013-09-14

In the next few posts I will show some string functions.
Maybe some of them can be useful. Anyway ...
BTW are you trying to recreate Total Commander "Rename Multipe Files" window? :)

Budi G · 2013-09-14

I only use the quick search did not elaborate on researching your script.

in your case:


(
if trailing_leading = false then
for i in selection do i.name = trimleft trailing_leading
)

I see in your script, the variable of 'trailing_leading' is a boolean, not a string

trimleft/trimright need a string (ie:name of object or any string)

for example to using them
a snippet from mxs help:


trimleft " \nMAXScript" --space and new line trimmed

--"MAXScript" -- it's result


trimright "$Teapot0911" "1234567890" --remove trailing numbers

--"$Teapot" -- it's result

or see example from barigazy

regards

Success sort of

3dwannab · 2013-09-14

Thanks for that. I did see that in the maxscript help but I didn't get it to work, however I did this time but with unfavoured reselts.

(
if trailing_leading = true then
for i in selection do i.name = trimright "Teapot123" "1234567890"
)

I have three objects:

Teapot123 | Lamp123 | Chair123

I selected those objects and ran the script. All three were named to Teapot. I can see what this is doing. But it's not what I was after. It also does this when the box is unticked. trailing_leading is a boolean for the box that you tick. If off names are untouched and on the trailing and leading spaces are removed which is what I was hoping for.

I know for an experienced maxscripter this would be a piece of cake but for my 1 weeks knowledge of this launguage it's a different story.

Attachments

barigazy · 2013-09-14

Because you use only "Teapot123" and you need to do it like this

if trailing_leading = true do
(
for i in selection do i.name = trimright i.name "1234567890"
)

barigazy · 2013-09-14

If you want to assigne one unique name to selection you can try this

-- u can use "02d" or "03d" formating
fn uniqSelectionNames arr title: prec: =
(
for i = 1 to arr.count do arr[i].name = title + formattedprint i format:prec
)
uniqSelectionNames selection title:"Boxes" prec:"03d"

Getting somewhere

3dwannab · 2013-09-15

if trailing_leading = false do
(
for i in selection do i.name = trimright i.name
for i in selection do i.name = trimleft i.name
)

Thank you.

This now works to trim unwanted spaces from left and right but when I run the script it instantly affects the names whereas I want it to toggle when I click/unclick the checkbox.

Further more it doesn't work when I type leading and trailing spaces in the upper left hand corner. And yes this is based on total commander. But obviously credit goes to 'AShim' which were his intentions from what I read over at http://www.scriptspot.com/3ds-max/scripts/rename-objects-extended

barigazy · 2013-09-15

shorter version

for i in selection do i.name = trimLeft (trimRight i.name)

3dwannab · 2013-09-15

Yes, I have and also Hyper renamer from TiM Scripts @ http://www.hyperent.com/Hyp-Maxscripts.php which I found to be great too.

I just wanted to use this script to learn as I like the fact you can save your settings to an INI file but I'll just have to cut my losses with this mod and try something easier to get started. Thanks for your help.

hi 3dwannab ...about

Budi G · 2013-09-15

hi 3dwannab ...
about trimleft/trimright,
Has the problem resolved ?
I see you've understood now, right ?

if you researching some script, you can use print() or format() function and check the mxs listener.

it's useful to get information of any variable,
such as what is result of the variable ?
You can apply this to another variables

it's maybe out of topic but still closest to your case.
if you not familiar with print() and format(), see example:


--------------- example random variables
got = true
counter = 45
arr = #(2,3,4,5,6,7,8)
str = "result ? 0,4,7"
trimstr = trimright str "?,047"
whatisthis = uniquename trimstr + (counter as string)
cc = (234*counter/5)
clearlistener() -- clear listener

---- Next ...you want to check several variables or hidden variables from your view

----------------------------------------- check some variable by print or format
format "%\n" whatisthis -- check a variable by format
-- or
print cc -- check a variable by Print

run this script then check to mxs listener

Is pressing the F1 key ...

3dwannab · 2013-09-15

while the flashing cursor is on the variable the same where it takes to to maxs help file or is your method different.

I'm finding it a bit overwhelming at the minute, thanks for your help

Opps..

3dwannab · 2013-09-13

See the script attached. It'll be much clearer then. Basically when the box is ticked to trim trailing/leading then it does just that.

You're probably familiar with this script but I've changed the UI slightly.

Attachments

barigazy · 2013-09-13

Not have time to look over the whole code.
Post me a snippet to see what is the problem.
Like this

let say i have en string *str*

--example#1 trimLeft | trimeRight fn
str = "--------------#### 3dwannab code ###--------------"
--to remove symbols you can use
str = trimLeft (trimRight str " ###--------------") "--------------#### "
-->result: "3dwannab code"

--example#2 replace fn
str = "--------------#### 3dwannab code ###--------------"
--define simbols that you want to remove
toDel = "-+*/|#$"
for i = str.count to 1 by -1 where (findstring toDel str[i]) != undefined do str=(replace str i 1 "") ; str
--> result:" 3dwannab code "

barigazy · 2013-09-13

Not quite understand what do you want to achive.
Just show me string before and after (result that you want)

johnsu6616 · 2018-05-04

Thank you so much.
Your information really helped me a lot!