Chopping a long string into smaller ones toy

I made a great tool I can't do without from now on! ;-D
Update#1: added null string checking, renamed "i" to "s".
Update#2: upgraded output format, added BASH version!

Here's the Haskell source code:

import System.Environment(getArgs)
import Data.List(unfoldr,intersperse)

chop k | k>0 = unfoldr f where
 f [] = Nothing
 f s  = Just $ splitAt k s

main = do
 [k,s@(_:_)] <- getArgs
 let out = show . concat . intersperse " "
     res = chop (read k) s
 putStrLn $ "filter(/=' ')" ++ out res



Typing:
./chop.hs 27 http://bkil.blogspot.com/2009/05/chopping-long-string-into-smaller-ones.html

Gives:
filter(/=' ')"http://bkil.blogspot.com/20 09/05/chopping-long-string- into-smaller-ones.html"

Or if you prefer, you could alternatively opt for the following BASH routine:

main(){
 printf 'echo "'
 printf "$2"|sed -r "s~.{$1}~& ~g"
 echo '"|sed "s~ ~~g"'
}



This one gives:
echo "http://bkil.blogspot.com/20 09/05/chopping-long-string- into-smaller-ones.html"|sed "s~ ~~g"

But where's the fun in that?? ;-)

Comments

  1. Just a small supplementary thought about an other - maybe more specific but not less common - string related task.
    To shorten URL strings always consider using URL shorteners. Of course they offer only URL compression but in this domain they are unbeatable. http://is.gd/LjT5

    ReplyDelete
  2. Howdy! Well, I think you've hit the nail on the head with this one. :-D However, you may recall that I had used these so called link shortening services even in their "dark ages". :-P

    According to your link, their drawbacks may outweigh the benefits in many situations. Though they may have no practical uses besides very space limited places like SMS and oneliners, I almost forgot how much fun they can be! ;-)

    ReplyDelete

Post a Comment

Popular posts from this blog

Tftp secret of TL-WR740N uncovered

Hidden TFTP of TP-Link routers

Haskell for embedded: C output, compilers, monads, Timber