Posted to tcl by Stu at Fri Mar 28 21:04:36 GMT 2008view raw

  1. # StrDivideAtStr --
  2. #
  3. # Divide string into two strings at index containing
  4. # divider string (default #).
  5. # Part of string containing divider string is discarded.
  6. #
  7. # Arguments:
  8. # str String to divide.
  9. # divStr String to match for division index.
  10. #
  11. # Results:
  12. # Always returns a list of two strings.
  13. # str result
  14. # ----------------------------
  15. # "abc#def" {"abc" "def"}
  16. # "abc" {"abc" ""}
  17. # "abc#" {"abc" ""}
  18. # "#def" {"" "def"}
  19. #
  20. proc StrDivideAtStr {str {divStr {#}}} {
  21. set index [string first $divStr $str]
  22. if {$index == -1} {
  23. return [list $str ""]
  24. }
  25. set s1 [string range $str 0 [incr index -1]]
  26. incr index [string length $divStr]
  27. set s2 [string range $str [incr index] end]
  28. return [list $s1 $s2]
  29. }
  30. ###
  31.