String Edits - Removing all characters after the 31st character

I’m trying to export schedules to excel and make the name of the sheet = name of the schedule. The issues is that sheet names in excel are limited to 31 characters and I have schedule names that are greater than 31 characters. The schedules with names greater than 31 characters don’t get exported.

To summarize: I’m looking to get rid of all remaining characters after the 31st character. I’ve tried various methods of removing the strings such as remove string and substring but I can’t seem to get it to work. Can someone lend me a hand?

Thank you!

This node or String.Substring(str,0,31) in a code block.

Thanks Jacob. That almost worked. I also have schedule names that aren’t above 31 characters. This script makes those indicines null

So… If the length < 31, return 31, otherwise return the length.

Take your String.Length, test that for > 31, and wire that into test input of an if node.
Put the String.Length into the false input and 31 into the true input.
The result of the if node becomes the ‘length’ input of the String.Substring node.

Or in a code block, something like this (not tested as I am not at my CPU):

baseLen = String.Length;
subLen = baseLen >31 ? 31 : baseLen;
newString = String.SubString(str,0,subLen);

I tried out both the methods and I was able to get the nodes to work! Thank you for your help!!

The if statement was close. I was fiddling with it (without entirely knowing what I’m doing) and got it to correct the < 31 values but the others were still coming up null.

Nonetheless, here’s the finished product for future reference:

Ah! Typo on my end - likely should be something like baseLen = String.Length(str);. Glad you’re sorted though!

1 Like