Re: Manipulating Strings
- To: mathgroup at smc.vnet.net
- Subject: [mg28287] Re: Manipulating Strings
- From: drnews at NOSPAM--Scientificarts.com (David Reiss)
- Date: Sat, 7 Apr 2001 03:44:46 -0400 (EDT)
- Organization: Scientific Arts
- References: <9ajmoi$sav@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <9ajmoi$sav at smc.vnet.net>, bghiggins at ucdavis.edu (Brian Higgins) wrote: > 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 In[1]:= squash2[str_String]:=StringJoin[Flatten[Union/@Split[Characters[str]]]] In[2]:= squash2["aaGghhhlmnopppWw123333"] Out[2]= "aGghlmnopWw123" -- ) Scientific Arts: http://www.scientificarts.com ) Creative Services and Consultation for the Applied ) and Pure Sciences:: David Reiss: dreiss at !SPAMscientificarts.com ) [Remove the !SPAM to send email]