RE: Manipulating Strings
- To: mathgroup at smc.vnet.net
- Subject: [mg28286] RE: [mg28248] Manipulating Strings
- From: "Harvey P. Dale" <hpd1 at nyu.edu>
- Date: Sat, 7 Apr 2001 03:44:44 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Brian: str1="CLKJLDiCXLklJDMXLKJdLmxLLLEIKXMDLKJcMDLKJXMeNNlB" StringJoin[Characters[str1]//.{a___,b_,b_,c___}\[Rule]{a,b,c}] Then the output is: CLKJLDiCXLklJDMXLKJdLmxLEIKXMDLKJcMDLKJXMeNlB Best, Harvey Professor Harvey P. Dale Director National Center on Philanthropy and the Law New York University School of Law Room 206A 110 West 3rd Street New York, N.Y. 10012-1074 tel: 212-998-6161 fax: 212-995-3149 -----Original Message----- From: bghiggins at ucdavis.edu [mailto:bghiggins at ucdavis.edu] To: mathgroup at smc.vnet.net Sent: Friday, April 06, 2001 1:53 AM To: mathgroup at smc.vnet.net Subject: [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