Skip to first unread message

WebAtelier- mt

unread,
May 10, 2017, 2:57:01 AM5/10/17
to MIT App Inventor Forum
How can I replace only 1 string in a text?

For example "Hello world". When I use the function /replace all text/segment/replacement/ and set "l" to "x" the text would be "Hexxo worxd"
But when I only want to change the third "l" that doesn't work. The text should be for example "Hello worxd"
Even when I use the segment start function I can only replace all occurrences...

Taifun

unread,
May 10, 2017, 10:11:50 AM5/10/17
to MIT App Inventor Forum
you can write your own custom procedure to replace only the n th character

Taifun 

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun. 

Abraham Getzler

unread,
May 10, 2017, 12:17:24 PM5/10/17
to MIT App Inventor Forum
See attached for how to do it.

The $ in the function names are a homage to BASIC, which uses $
in its text function names.

ABG

mid$.PNG
replace$.PNG
replace$_test.PNG
text.aia

Taifun

unread,
May 10, 2017, 12:38:12 PM5/10/17
to MIT App Inventor Forum
thank you ABG!

@Hossein: what about adding a method like that into your TextUtils extension?

Taifun

WebAtelier- mt

unread,
May 11, 2017, 9:50:34 AM5/11/17
to MIT App Inventor Forum
Thanks,

but I'm looking for some more diffuicult solution.

I have a list "1,2,3,4,5,6,7,8,9,0,a,b,c,d,,A,B,C,D,?,(,(" and a second with "a,b,c,d,e,f,g,h,i,j,...1,2,3,4,5..."

I now want to replace the things in the first list with the things in a second list. For example 1 becomes a, 2 becomes c etc.
I've already done it wit a text that I sent to a list. Then I replaced the things. (If I only have letters, it works)
But there's somehow a problem, if numbers and letter are mixed. So it comes to a wrong result...

Any idea?

Italo

unread,
May 11, 2017, 9:54:32 AM5/11/17
to MIT App Inventor Forum
You forgot the screenshot of your blocks. You say it works with letters but not with numbers, so there must be something to fix but without looking at your blocks, probably no one can help.

Abraham Getzler

unread,
May 11, 2017, 11:47:48 AM5/11/17
to MIT App Inventor Forum
This looks like a substitution cipher, where the two lists are just columns 1 and 2 in a map table.

There is a very handy lookup in pairs block that can do single letter translations from such a map.

result procedure ENCODE(text_input, map) returns(text_output)
 result text_output = ""
 for i = 1 to length(text_input)
   set local letter to segment(text_input,i,1)
   set text_output to JOIN(text_output, lookup_in_pairs(map,letter))
end for
return text_output
end procedure ENCODE

Use a spreadsheet to prepare your map,
export it as a csv file, upload it to Media drawer,
load it from file //map.csv at startup.

ABG


Reply all
Reply to author
Forward
0 new messages