String.Replace search for precise name issue


#1

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)


#2

string replace is returning 10
see 18 onder string replace node


#3

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.


#4

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


#5

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:


#6

solves this youre problem?

String_replace_error_1.dyn (23.5 KB)


#7

@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)


#8

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:


#9

@JacobSmall 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.

String_replace_error_4.dyn (17.0 KB)


#10

@Julian_Krab See if this works …


#11

@Julian_Krab this could work for you


#12

My method expanded and explained some:


#13

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