cut off part of a string

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

cut off part of a string

Post by Holladerwaldelf »

I have "related" strings that look like
var1 = "C1_String1", var2 = "C1_String2", var3 = "C1_String3", ...
I would like to cut off the start-substring "C1_" and put the remainder into new variables that will look like
newVar1 = "String1", newVar2 = "String2", newVar3 = "String3", ...
At first glance I do not seem to be able to use the substring-function as I do not know the length of the start-substring (it could also be something like "AB1_" or "C23_") and I do not know the length of the entire string. So I do not know the start- and end-indices of the substring that interests me.
There is probably an easy way to do this but I did not find anything in the documentation or the forum...
Thanks for any help


Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

Re: cut off part of a string

Post by Holladerwaldelf »

Thank you!
I was hoping there already existed a function that does what i want :D
Now I wrote my own trim-function:

Code: Select all

[h: '<!-- 	FUNCTION
			string.trimStart (String str, String separator) 
			return: String -->' ]

[h: macro = "string.trimStart" ]
[h: macro.arguments = "(str,separator)" ]
[h: macro.noArgs = 2]
[h: '<!-- 	variables to get a nice output for error messages -->']
[h: macro.type = "function" ]
[h: this = getMacroLocation() ]
[h: macro.name = strformat("%{this}.%{macro}%{macro.arguments}") ]
[h: '<!-- test if number of arguments is correct -->']
[h: input.noArgs = argCount()]
	[h: errorMessage = strformat("<b>%{macro.type} %{macro.name}</b> needs <i><b>%{macro.noArgs}</b> </i>argument(s)<i> (input arguments:%{input.noArgs})</i>")] 
	[h: assert( input.noArgs == macro.noArgs, errorMessage ) ]
[h: '<!-- get the arguments -->']
[h: input.str = arg(0)]
	[h: errorMessage = strformat("%{macro.type} <b>%{macro.name}</b>: input.str is empty.") ] 
	[h: assert ( !json.isEmpty(input.str), errorMessage) ]
[h: input.separator = arg(1) ]
	[h: errorMessage = strformat("%{macro.type} <b>%{macro.name}</b>: input.separator is empty.") ] 
	[h: assert ( !json.isEmpty(input.separator), errorMessage) ]
[h: '<!-- -------------------------------------------- -->']

[h: local.index = indexOf(input.str,input.separator)]
[h: local.length = length(input.str)]
[h: return.str = substring(input.str,local.index+1,local.length)]

[h: '<!-- -------------------------------------------- -->']
[h: macro.return = return.str]
If there is an easier way to do it I would appreciate if someone showed me.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: cut off part of a string

Post by aliasmask »

You can also use listget and use "_" as a delimiter.

Code: Select all

[H: newVar = listget(var,1,"_")]

Post Reply

Return to “Macros”