Re: Manipulating Strings
- To: mathgroup at smc.vnet.net
- Subject: [mg28283] Re: [mg28248] Manipulating Strings
- From: BobHanlon at aol.com
- Date: Sat, 7 Apr 2001 03:44:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
squash[str_String] := StringJoin[First /@ Split[Characters[str]]]; Bob Hanlon In a message dated 2001/4/6 2:23:33 AM, bghiggins at ucdavis.edu writes: >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]]] >