Search and replace just the first character

I’m trying to replace the letter A but only if it’s the first character in a sheet number. Any ideas?

What have you tried so far?

Thanks for the reply. Here’s what I have so far.

String.Replace will replace any instance of your input. In your case “A-00-A01” will become “AA-00-AA01”.
Try to use node String.StartsWith to filter those sheets that their number starts with “A”.
Then, you can use String.IndexOf and String.Remove and String.insert to replace the start of the strings, for example.

Thanks again for the reply. Sounds easy but I’m a beginner. Not sure how to use String.IndexOf.

String.IndexOf will return the index/position of the first occurrence of the character/s looked for. This will help to locate on the string where to remove/add your value.
Depending on your numbering convention, it can be easier if they start with “A-” for example.

See picture and file attached, hope it helps :slight_smile:


sheetNumberReplace.dyn (19.5 KB)

Thank you so much!!! I’ll look at it and see if I can get it going. MUCH appreciated!

For some reason some of the sheets are getting skipped. Any idea why? Thank you.

Capture

Here’s my graph.

I’d include the dyn but the forum won’t allow new users to upload dan files.

Search your library for substring. Use that node to get the first character (index 0 length 1).

Use a code block to replace anything that is just A with AA as follows:

val == “A”? “AA”: val

Then use the substrig node to get everything after the 1st character. Hint: you’ll need to get. The string length for this.

Then use a + node to add the results of the code block to the rest of the string.

Give it a shot and see where you get.

In your screen capture above you need to pass your list of sheet elements through a List.FilterByBoolMask node using the same mask as the sheet numbers, so you are setting the parameter value for the correct elements

Also here is an alternate method just for demonstrative purposes using an If node that doesn’t require you to filter the sheets out from eachother:

Thanks for the reply. That’s very close to what I’m looking for. I have sheets numbered A-100…, I want to replace the A- with AA. Currently the graph changes A-100 to AA-100. I’m hoping to get it changed to AA100. Thanks Again!

Replace A- with AA. This solves your “first letter only” predicament and gives you the correct format. This is the exact circumstance I usually look for. Sometimes you can get away with searching for more than the part you-'re trying to replace and get more control out of your script.

Hmmm… When I do that I get AA–. Not sure what I’m doing wrong.

Hi
Do you know why some changed and some did not? I can’t figure it out. Thank you!

Could this work?

Hi @BBDCW, as the others explained, there are several different ways to approach your problem, but all depend on how generic or specific you need your solution to be.

Sticking with the filtering method, you actually need to “filter in” the sheet elements themselves rather than just the sheet names, and do the replacement only on those:

.

This approach would cover any kind of input to be replaced (in theory). If you are looking for something more specific (all your sheet numbers start with “A-” for example), any of the other methods mentioned should work just fine.

Cheers!
sheetNumberReplace.dyn (17.5 KB)

That worked perfectly. Now I need to reverse engineer it so I understand all of it. Thanks to everyone that helped me!