Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Manipulating Strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28248] Manipulating Strings
  • From: bghiggins at ucdavis.edu (Brian Higgins)
  • Date: Fri, 6 Apr 2001 01:52:56 -0400 (EDT)
  • Organization: The Math Forum
  • Sender: owner-wri-mathgroup at wolfram.com

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


  • Prev by Date: Re: Special Matrix
  • Next by Date: Re: C, MathLink or Java, J/Link
  • Previous by thread: Re: Manipulating Strings
  • Next by thread: Re: Re: Manipulating Strings