Help with the trimleft/trimright function

 

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
3dwannab's picture

I know this is an old thread

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"
barigazy's picture

*CORRECT FILENAME*

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"

bga

barigazy's picture

*CLEAN DUBLE PUNKCTION MARKS*

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,-"

bga

barigazy's picture

*FIND AND REPLACE ALL IDENTICLE WORD*

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"

bga

barigazy's picture

*FIND AND REPLACE FIRST IDENTICLE WORD*

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"

bga

barigazy's picture

*FILTER NAME NUMBERS*

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"

bga

barigazy's picture

*CLEAN STRING*

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"

bga

barigazy's picture

*CAPITALIZE ONLY FIRST LETTERS*

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"

bga

barigazy's picture

...

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? :)

bga

Budi G's picture

I only use the quick search

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.