Hello,
i am new to smalltalk and i look for some easy way to find some substring in another string and replace it by another substring: example: string := 'Hello how are you? I am fine. Thank you and you?' substring to find 'you' replace by 'AA' transform to 'Hello how are AAA? I am fine. Thank AAA and AAA?' thnx. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Open up a method browser and search for:
findSubstring: in: startingAt: matchTable: which is a method for ByteStrings and then look at replaceFrom: to: with: startingAt: I'm still new too so hope it works for you. - Robert On Sun, 2007-12-30 at 13:23 +0100, an organic wrote: > Hello, > > i am new to smalltalk and i look for some easy way to find some > substring in another string and replace it by another substring: > > example: string := 'Hello how are you? I am fine. Thank you and you?' > substring to find 'you' replace by 'AA' > > transform to 'Hello how are AAA? I am fine. Thank AAA and AAA?' > > thnx. > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Jakub-8
On Sun, Dec 30, 2007 at 01:23:49PM +0100, an organic wrote:
> Hello, > > i am new to smalltalk and i look for some easy way to find some substring in > another string and replace it by another substring: > > example: string := 'Hello how are you? I am fine. Thank you and you?' > substring to find 'you' replace by 'AA' > > transform to 'Hello how are AAA? I am fine. Thank AAA and AAA?' You can search for suitable methods using the Method Finder (world menu -> open... -> method finder). Try looking for methods with names that contain 'replaceall' or names that contain "copyreplace". In Smalltalk, it is common practice to copy strings rather than modify characters within a string, so you will find methods such as #copyReplaceAll:with: Here is one way to do what you want: 'Hello how are you? I am fine. Thank you and you?' copyReplaceAll: 'you' with: 'AAA' asTokens: false If you know that the substrings that you want to replace are all going to be "tokens" (surrounded by non-alphanumeric characters) then you can use this to achieve the same result: 'Hello how are you? I am fine. Thank you and you?' copyReplaceTokens: 'you' with: 'AAA' Note that in both cases, the method leaves the original string intact and answers a new string with the substrings replaced, so you might use it like this: originalString := 'Hello how are you? I am fine. Thank you and you?'. modifiedString := originalString copyReplaceAll: 'you' with: 'AAA' asTokens: false. Dave _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Jakub-8
Print it:
string copyReplaceAll: 'you' with 'AA' Literal strings are mutable by not in length so you can easily do destructive changes in some special cases where you're sure that length doesn't change. Thus the following is ok: string replaceAll: $a with: $b I think mutable literal strings are a bad thing. Read the new Squeak bible: http://www.iam.unibe.ch/~scg/SBE/index.html (especially around pages 207-209) Cheers, -- Martial an organic a écrit : | Hello, | | i am new to smalltalk and i look for some easy way to find some substring in | another string and replace it by another substring: | | example: string := 'Hello how are you? I am fine. Thank you and you?' | substring to find 'you' replace by 'AA' | | transform to 'Hello how are AAA? I am fine. Thank AAA and AAA?' | | thnx. | _______________________________________________ | Beginners mailing list | [hidden email] | http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |