IonicWind Software

IWBasic => General Questions => Topic started by: Brian on April 13, 2021, 12:09:37 PM

Title: Printer Names
Post by: Brian on April 13, 2021, 12:09:37 PM
Help! I have a string of printer names, separated by a semi-colon. How can I split the string up to put the printer names in a listbox with ADDSTRING?

Brian
Title: Re: Printer Names
Post by: billhsln on April 13, 2021, 04:43:12 PM
I would think that you could split them up the same way you would do either a tab delimited or a comma delimited file.

Send me a sample.

Bill
Title: Re: Printer Names
Post by: ckoehn on April 17, 2021, 01:28:21 PM
Here is what I use to use.  I'm sure it can be improved.

SUB Split(STRING txt,STRING ch,string words[]),INT  'number of entries in the array
        'Split ( text to split, chars to split at, array to store entries)
INT i,cnt=0
string item=""

FOR i=1 to LEN(txt)
IF INSTR(ch,MID$(txt,i,1))>0 and item<>"" and cnt<(SIZEOF(words))
words[cnt]=item
cnt++
item=""
ELSE
item+=MID$(txt,i,1)
ENDIF
NEXT i

IF item<>""
words[cnt]=item
ELSE
cnt--
ENDIF

RETURN cnt
ENDSUB

Later,
Clint
Title: Re: Printer Names
Post by: billhsln on April 17, 2021, 01:38:00 PM
Here is another quick way to split a string:

i = -1
j = InStr(plist$,";")
do
  if j > 0
    i++
    word[i] = mid$(plist$,1,j-1)
    plist$ = mid$(plist$,j+1)
    j = InStr(plist$,";")
  endif
until j = 0
i++
word[i] = plist$

Bill
Title: Re: Printer Names
Post by: ckoehn on April 17, 2021, 02:49:28 PM
I have used that method too Bill.  It works really fast when splitting for just one char like a comma or semi comma like Brian was probably needing.  I would recommend it when having to split a strings needing only one char to split it.

The one I posted can split with a string of different chars, like this: item_cnt = Split("item1,item2;item3.item4", ",;.", words[]) would split into words[0] = "item1", words[1] = "item2", words[2] = "item3", words[3] = "item4".

Isn't programming fun.

Later,
Clint