String.Replace search for precise name issue

I have an issue where I try to String.Replace words that starts the same way, f.ex. “Level 1” and “Level 10”.
So when I have “Level 10”, String.Replace recognizes it as “Level 1” and replaces the string according to “Level 1”.
So when String.Replace is searching for “Level 1”, it also detects “Level 10” and replaces it accordingly to Level 1.
How do I avoid this?

String_replace_error_2.dyn (11.5 KB)

string replace is returning 10
see 18 onder string replace node

Sorry @Nico_Stegeman, I might have explained it wrongly. Let me make an edit.
You are right, when it comes to replacing “Level 10”, it does replaces it correctly. But when it comes to replacing “Level 1”, then it also replaces strings “Level 10” because it actually contains that value it is searching for.
See picture with yellow marks.
Capture_2

I have updated the original post with simplified Dynamo script and picture focusing on the problem :slight_smile:

Not at the PC but you could assess another condition in the form of “String.Length” to check if the value Level 10 is longer than Level 1. Kind of like a sub filter.
There will be lots of other ways to do this of course. :grinning:

2 Likes

solves this youre problem?

String_replace_error_1.dyn (23.5 KB)

@Nico_Stegeman no, it does not solves the problem. I cannot use First.Item because sometimes I have many “Level 1” strings with other suffixes. See picture.

String_replace_error_3.dyn (12.9 KB)

Splitting the string at the separator for the prefix and suffix, and asking for equality is likely the way to go here.

Something like this:

List.Clean(
	List.GetItemAtIndex(String.Split(X, " - ")<1>,1)==SearchTerm?
		X:
		null,
	false
);

Which looks like this when trying to only use nodes:

@jacob.small thank you, but not sure how this helps me. You seem to focus on getting strings containing “Level 1” , but I also though need to rename all other strings. Here is picture and .dyn with your method.
Capture_5

String_replace_error_4.dyn (17.0 KB)

@Julian_Krab See if this works …

1 Like

@Julian_Krab this could work for you

1 Like

My method expanded and explained some:

5 Likes

Interesting method @Raja , with some adjustment it also works!

1 Like