How do I filter a string including empty entries?

MAXScript Frequently Asked Questions

A user asked:

The filterString function does not return empty entries. For example, if I am filtering by tabs a string containing two tabs in a row, no empty entry will be returned.

For example,

filterString "One\t\tThree Four,,Five " "\t, "

#("One", "Three", "Four", "Five")

Answer:

The following scripted function behaves as requested.

 

Scripted Function:

 

fn filterString2 theString theDelimiters =

(

 theTokens = #() --array of tokens to return

 ready = false

 while not ready do --repeat until no more delimiters can be found

 (

  ready = true --raise a flag that we are done

  thePosArray = #() --init. an array to hold possible split positions

  for i = 1 to theDelimiters.count do --go through all delimiters

  (

   checkPos = findString theString theDelimiters[i] --check if the delimiter is in the string

   if checkPos != undefined do append thePosArray checkPos --if it is, add to the split positions array

  )--end i loop 

  if thePosArray.count > 0 then --if the array has any split positions,

  (

   sort thePosArray --sort the array in ascending order

   checkPos = thePosArray[1] --grab the first position

   aToken = substring theString 1 (checkPos-1) --take the token from start to the split pos. - 1

   append theTokens aToken --add the token to the array of tokens

   theString = substring theString (checkPos+1) theString.count --remove the token and the delimiter from the string

   ready = false --lower the flag because we are not ready yet

  )--end if

 )--end while loop

 append theTokens theString --add what is left of the string to the array

 theTokens --return the array of tokens

)

 

filterString2 "One\t\tThree,Four,,Five " "\t,"

#("One", "", "Three", "Four", "", "Five ")

 

filterString2 "One\t\tThree Four,,Five " "\t, "

#("One", "", "Three", "Four", "", "Five", "")

 

Note:

In 3ds Max 8, a new keyword parameter splitEmptyTokens has been added to the filterString function to perform this kind of filtering.

 

See also

Frequently Asked Questions