Split a character string based on change of character: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 1,183: | Line 1,183: | ||
return ret |
return ret |
||
end function</lang> |
end function</lang> |
||
=={{header|FutureBasic}}== |
|||
FB can process either Pascal strings (slowly being deprecated), or Apple's Core Foundation CFStrings (and Objective-C NSStrings). Here's the old-school Pascal string function: |
|||
<lang> |
|||
local fn SplitString( inputStr as Str255 ) as Str255 |
|||
Str255 resultStr |
|||
NSUInteger i |
|||
if len$( inputStr ) < 2 then resultStr = inputStr : exit fn |
|||
resultStr = left$( inputStr, 1 ) |
|||
for i = 2 to len$( inputStr ) |
|||
if mid$( inputStr, i, 1 ) <> mid$( inputStr, i - 1, 1 ) then resultStr = resultStr + ", " |
|||
resultStr = resultStr + mid$(inputStr, i, 1) |
|||
next |
|||
end fn = resultStr |
|||
window 1 |
|||
print fn SplitString( "gHHH5YY++///\" ) |
|||
HandleEvents |
|||
</lang> |
|||
And here's the recommended CFString counterpart: |
|||
<lang> |
|||
local fn SplitString( inputStr as CFStringRef ) as CFStringRef |
|||
CFStringRef resultStr |
|||
NSUInteger i |
|||
if len( inputStr ) < 2 then resultStr = inputStr : exit fn |
|||
resultStr = left( inputStr, 1 ) |
|||
for i = 2 to len( inputStr ) |
|||
if mid( inputStr, i, 1 ) <> mid( inputStr, i - 1, 1 ) then resultStr = fn StringWithFormat( @"%@, ", resultStr ) |
|||
resultStr = fn StringWithFormat( @"%@%@", resultStr, mid( inputStr, i, 1 ) ) |
|||
next |
|||
i = len( resultStr ) - 2 |
|||
resultStr = fn StringByReplacingCharactersInRange( resultStr, fn CFRangeMake( i, 1 ), @"" ) |
|||
end fn = resultStr |
|||
window 1 |
|||
print fn SplitString( @"gHHH5YY++///\\" ) |
|||
HandleEvents |
|||
</lang> |
|||
'''Output for either function:''' |
|||
<pre> |
|||
g, HHH, 5, YY, ++, ///, \ |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,217: | Line 1,267: | ||
g, HHH, 5, YY, ++, ///, \ |
g, HHH, 5, YY, ++, ///, \ |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |