//Title: Compress Text
//Credit To: Popisfizzy
/*
This compresses a text string by removing leading and trailing
spaces. For example, the string " a " will become "a", and
" Hello, how are you? " becomes "Hello, how are you?"
*/
proc/compress_text(string)
var/len = length(string)
var/e, f
for(var/a = 1, a <= len, a ++)
if(text2ascii(string, a) != 32)
if(!e)
e = a
a --
else f = a + 1
if(!e && !f) return ""
if(e) return copytext(string, e, f)
return string
//Testing implimentation
mob/verb/Test(string as text) world << compress_text(string)
ID:195067
Jun 20 2007, 7:21 pm
|
|
Jun 21 2007, 5:11 pm
|
|
Interesting. My solution to this problem involved my substring matching algorithms. I would count the number of spaces at the front and end of the string (each stops iterating as soon as it encounters a symbol that doesn't match), then copy the part of the string that excluded those.
|
//Title: Compress Text |