Replace specific character within parameter value

Hello everyone

Revit Version: 2023
Dynamo Core: 2.13.1.3887
Dynamo Revit: 2.13.1.3891

I’m trying to replace the fifth character of a parameter value with specific characters. Below is the mapping for replacements.

Due to restrictions on software and downloads within my organisation I am currently only able to use the native Dynamo nodes.

  • 0 → (nothing)
  • 1 → A
  • 2 → B
  • 3 → C
  • 4 → D
  • 5 → E
  • 6 → F

I’ve encountered some issues with my script:

  1. The Element.ToString node returns ‘Function’ and doesn’t work with String.Split.
  2. How can I split the string into its indices?
  3. How can I map the characters for replacement?

Any guidance would be appreciated! Let me know if you need more information.

You’re not providing enough inputs to Element.ToString or String.Split. A red input on a node means that you need to provide a value.

You should just be able to use String from Object instead of Element.ToString and a blank string ("") as the separator for String.Split. That will give you each of the string characters at an index that you can then replace via a dictionary and rejoin.

Hi Nick

Thanks for your reply, I will update the script with your suggestion. I’ll let you know how I get on.

I will have a go at creating the dictionary myself before I come back for any more help.

Hi Nick

I have made some good progress from yesterday, I have attached an screenshot of my updated script. Thanks for your help so far.

I had to add a String from Object again after String.Length for items to appear in the watch list.

I have managed to use the String.Split node which has split the lists into individual lists.

I now want to break each entry down into its individual characters so I can target the fifth character and replace it with the letter I need. I have had a look online and can’t seem to work this out.

If you use dictionaries like @Nick_Boyts suggested you don’t need
have to “target the fifth character

Hi @bvs1982,

Thanks for your response! I was planning to split my building numbers into individual characters before moving on. Are you suggesting that this step might not be necessary?

Also, is the screenshot you shared an example of a dictionary, or does it specifically address changing the fifth character like I need to?

You have to Split
But if it is allways the fifth character then that character would be allways at index 4.

You could use do multi IF statements in a Code Block (instead of a Dictionary) like so :point_down:.

try this:
renameFifthChar.dyn (7.8 KB)

Though it is really nice to post your .dyn, i wonder how much OP
will learn from a .dyn with Python in it (which does it all) :sweat_smile:.

@bvs1982

This has been extremely helpful, and have achieved what I needed.

I now want to input all my building number into this instead of the Code Block I have used as an example. I have given this a quick go and my index is looking at the fourth building number not the fourth character of each building number.

I am very early on in my Dynamo learning looking at code is going to blow my mind at this point.

You still have to Split and i guess you entered Levels and Lacing territory ;).
Give me a moment and see if i can come up with some examples.

Hi again

Please see the screenshot attached, I have linked my building numbers directly into the String.Split node and used a blank string for the separator.

I am getting an error when the script reaches Dictionary.ValueAtKey.

My first guess is you miss 0 (zero).
Add that to your Dictionary.

So you have (key → value);

  • 0 → 0
  • 1 → A
  • 2 → B
  • 3 → C
  • 4 → D
  • 5 → E
  • 6 → F

List levels are definitely the next step. Using a dictionary to update the value of the 5th character (4th index) makes that step trivial and then you just replace the old value with the new one and re-join.

1 Like

I’m not sure this is the issue as the script outputs exactly what I want before I inpute my building number list.

Using the the test Code Block the building number is split into it’s 5 characters and I am able to specify the fourth index and replace it with the relevant letter.

Inputting my building number list the List.GetItemAtIndex node picks out the fourth index which gives me the fifth entry in the list.

I will post the results of this in a separate reply.

I need to somehow break down each building number into a list of then of characters and then find the index.

@Nick_Boyts made a great example of everything I was saying.
Just replicate that and you should be good to go @fintan.sumners.

Hi Nick

Thanks very much for your reply I will replicate this and see what the results are.

Why would my building number list created from “Element.GetParamterValueByName” be acting differently to a Code Block with the same numbers.

You need to use List Levels to specify that you want the 4th index of each sublist. Right now you’re just getting the 4th sublist which is the 4th building number. The difference is the list structure you’re providing, not the code block or its values.