Re: Manipulating Strings
- To: mathgroup at smc.vnet.net
- Subject: [mg28289] Re: [mg28248] Manipulating Strings
- From: Matt.Johnson at autolivasp.com
- Date: Sat, 7 Apr 2001 03:44:48 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Brian- In[31]:= str = "aaabccd55kleeeee" Out[31]= "aaabccd55kleeeee" In[32]:= squash2[string_String] := StringJoin[Characters[string] //. {{h___, x_, x_, t___} -> {h, x, t}}] In[33]:= repeatedCharReplace[str] Out[33]= "abcd5kle" -matt bghiggins at ucdavis.edu (Brian Higgins) on 04/05/2001 11:52:56 PM cc: Subject: [mg28289] [mg28248] Manipulating Strings Dear Math group, I have a string with runs of repeated characters and want to replace sequences of repeated characters with a single character. For example consider the string "CLKJLDiCXLklJDMXLKJdLmxLLLEIKXMDLKJcMDLKJXMeNNlB" Thus I want a function that will take the groups "LLL" and "NN" in the above string and replace them with "L" and "N". Now I have been able to devise such a function but it seems pretty clunky, and I was wondering if anyone had a better idea tha made use of StringMatchQ to do the same task. Here is my method squash[str_String] := Module[{repeatCharList, repeatPattern, repeatChar}, repeatCharList = Select[Split[Characters[proteinString2]], Length[#] > 1 &]; repeatPattern = Apply[StringJoin, repeatCharList, 1]; repeatChar = Map[First, repeatCharList]; StringReplace[str, Thread[repeatPattern -> repeatChar]]] Thanks much, Brian ************************************************************************************************************************************** All Autoliv e-mails remain Company property and shall be used for business-related purposes only. Do not forward any material contained within this e-mail without prior, written permission from the author's manager. Autoliv disclaims all responsibility and accepts no liability (including negligence) for the consequences for any person acting, or refraining from acting on this information prior to the receipt by those persons of subsequent written confirmation. **************************************************************************************************************************************