ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Multi-character split
Submitter: Jeff Hobbs (other recipes)
Last Updated: 2001/09/10
Version no: 1.0
Category: Text Processing

 

5 stars 1 vote(s)


Approved

Description:

The split command splits a string based on each character that is in the splitString. This version handles the splitString as a combined string, splitting the string into constituent parts.

Source: Text Source

# mcsplit --
#
#   Splits a string based using another string
#
# Arguments:
#   str       string to split into pieces
#   splitStr  substring
#   mc        magic character that must not exist in the orignal string.
#             Defaults to the NULL character.  Must be a single character.
# Results:
#   Returns a list of strings
#
proc mcsplit "str splitStr {mc {\x00}}" {
    return [split [string map [list $splitStr $mc] $str] $mc]
}

The license for this recipe is available here.

Discussion:

This one-line multi-character split is simple and efficient. The splitStr is mapped into the single magic character which can then be passed to split for splitting the string. Be careful to make sure your string does not have NULLs, or to use a different magic character. Perhaps an undefined Unicode character would also be a good default magic character.



Add comment

No comments.



Highest rated recipes:

1. With busy cursor

2. Get widget info

3. Simplified mega-widiget ...

4. Supporting mouse wheel ...

5. Multi-character split

6. LCD Number Display

7. Check creditcard numbers ...

8. A minimal debugger

9. Socket based communicatio...

10. WSCP - showServerStatus




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.