Search a DC attribute string value  Topic is solved

Search a DC attribute string value

Postby hank » Tue Jun 20, 2017 3:38 am

If you wanted to see if the string "Hello" was in the following attribute string value...

qwertyHello12345

How would you do it?

The trick is, can you return one value (like "TRUE" or "1") if found and another (like "FALSE" or "0") if not?

I would be interested in a Ruby solution too if necessary!
0

hank 
 

Re: Search a DC attribute string value

Postby pcmoor » Wed Jun 21, 2017 4:25 am

Add the required text to the end of the string then use "find", then compare the length of the original string and position returned by find. By adding your text to the end, find will always return a value
1

pcmoor 
 

Re: Search a DC attribute string value

Postby hank » Thu Jun 22, 2017 2:20 pm

Thanks pcmoor.

So... trying to understand this. You have:
Code: Select all
=if((find("hello",Astring&"hello",1)-len(Astring))<0,"true","false")


where Astrin is set to

Code: Select all
hellggggheloxxhell


So in other words

Code: Select all
=if((find("hello","hellggggheloxxhell"&"hello",1)-len("hellggggheloxxhell"))<0,"true","false")


Which I think means:

If FIND returns a number that is greater than the length of the search string, return true.


So you are sort of forcing FIND to return something if the search string is not found by a certain position by adding the search string onto the end of the search haysatck,right?

I'm not sure I understand completely but this seems like some ninja stuff!
0

hank 
 

Re: Search a DC attribute string value  Topic is solved

Postby pcmoor » Fri Jun 23, 2017 12:19 am

More or less correct. As you know find returns a position but that is -1 (an error) if there is no instance of the required text. So adding the text you are trying to find to the end of the original string, then if there is no other instance, then position return will not be -1 but the length of the original string plus one,
So the difference between (position-len(original string)) returns a negative number if another instance is found, which if<0 will return true



added the above example with extra attributes to show steps clearer. but better to use one liners to save space in your DC dialog, once understood
1

pcmoor 
 

Re: Search a DC attribute string value

Postby hank » Sat Jun 24, 2017 3:56 pm

YES! I see now. That is some pretty sweet re-engineering of the pretty remedial string functions! I do wish FIND would just return FALSE if the string is not found but oh well. Thanks Again pcmoor!
0

hank 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Dynamic Components

Who is online

Users browsing this forum: No registered users and 1 guest

Visit our sponsors: