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
cut off part of a string
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Re: cut off part of a string
here's everything you need:
http://lmwcs.com/rptools/wiki/Category:String_Function
Specifically, to solve your issue, you need:
Wiki: indexOf()
Wiki: length() and
Wiki: substring()
http://lmwcs.com/rptools/wiki/Category:String_Function
Specifically, to solve your issue, you need:
Wiki: indexOf()
Wiki: length() and
Wiki: substring()
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
-
- Cave Troll
- Posts: 74
- Joined: Fri Aug 07, 2015 1:16 pm
Re: cut off part of a string
Thank you!
I was hoping there already existed a function that does what i want
Now I wrote my own trim-function:
If there is an easier way to do it I would appreciate if someone showed me.
I was hoping there already existed a function that does what i want
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]
Re: cut off part of a string
You can also use listget and use "_" as a delimiter.
Code: Select all
[H: newVar = listget(var,1,"_")]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools