Custom Node: How to get a specific group of characters from a list of strings?

Hello! and Thank you very much in advance for your help.

I have been trying to create a Custom node in Dynamo with a code in C# that I used before to extracting a specific group un characters inside of a string. This group of characters are inside of “”. I used these lines of code (left green) before and worked in a console, but when I try to used them in Dynamo does not work. I tried to make a custom code block and also a custom node but any worked. Please, Could you let me know where is the error? Thank you very much.

def cutString= (firsStringPosition,secondStringPosition)

objectName = (v);
firstStringPosition = (objectName.IndexOf(“[”));
secondStringPosition = (objectName.IndexOf(“]”));

stringBetweenTwoStrings =( objectName.Substring(firstStringPosition+1,
secondStringPosition - firstStringPosition-1));

return stringBetweenTwoStrings;

Codeblocks in Dynamo allow the use of DesignScript, the language which runs under the hood in Dynamo. This is not C#, or C, or Python but a language of it’s own. As such what works in C#, C or Python or whatnot will not necessarily run in Dynamo.

You can do this a few different ways - my preference would be to split the original string at the character in question. In design script it’d be something like this (note I am not at my PC so I can’t test this for typos - pardon any mistakes):

firstSplit = String.Split(originalString,splitter1)[1];
secondSplit = String.Split(firstSplit,splitter2)[0];

This can be combined into a single line of design script thusly:
String.Split( String.Split(originalString,splitter1)[1],splitter2)[0];

There is no need for a custom definition if called this way as it’ll be equally fast as a single line statement, but if you’re using it frequently you can define a custom tool for it something like this (it’s been awhile since I’ve coded a function like this - check for typos closely and save before executing/calling it):

def findInBetween (originalString, splitter1,splitter2)
firstSplit = String.Split(originalString,splitter1)[1];
secondSplit = String.Split(firstSplit,splitter2)[0];
return = secondSplit;

Once that is defined in another code block you can call it in newer code blocks like this: findInBetween(originalString,”[“,”]”);. I bring up the newer code block as previously placed code blocks don’t see definitions in more recently placed code blocks to decrease runtime lag. As such you may have to recreate newer code blocks.

1 Like

Thank you very much for your help!! is much clear now!! :slight_smile:

1 Like