Split string, Get first value

Split string, Get first value

dalton98
Collaborator Collaborator
1,175 Views
8 Replies
Message 1 of 9

Split string, Get first value

dalton98
Collaborator
Collaborator

Hello im looking for this vba code in AutoLisp. Ive looked at some examples of this but just cant wrap my head around it. Thanks.

Dim string1 As String
Dim list1 As String()
string1 = "hello world number 1"

list1 = string1.Split(" ")

MessageBox.Show(list1(0))

'returns: "hello"

 

0 Likes
Accepted solutions (1)
1,176 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

@dalton98 wrote:

.... Ive looked at some examples of this but just cant wrap my head around it. ....


Does that mean you found examples, but didn't understand what they do, or how to use them?  Any response you get now is probably going to repeat the same procedures used in those examples (there are many in this Forum), so will that help?  Can you explain what you didn't understand?

Kent Cooper, AIA
0 Likes
Message 3 of 9

dalton98
Collaborator
Collaborator

The examples said they were 'recursive' and looks like they built the split string with a loop. I really just need the text left of the first space. Wasn't sure if splitting the string was the best way to go about doing this.

0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@dalton98 wrote:

.... I really just need the text left of the first space. ....


That's simpler:

 

Command: (setq str "This is a test.")
"This is a test."

Command: (substr str 1 (vl-string-position 32 str))
"This"

 

[32 is the ASCII character code for a space.  You may need to put in (vl-load-com) to ensure the (vl-string-position) function is available, before trying it.)

Kent Cooper, AIA
0 Likes
Message 5 of 9

dalton98
Collaborator
Collaborator

Thank you. That's very simple to use

0 Likes
Message 6 of 9

ronjonp
Mentor
Mentor

Another using vl-string-search

(defun _getfirstword (str / i)
  (if (setq i (vl-string-search " " str))
    (substr str 1 i)
    str
  )
)
(alert (_getfirstword "hello world number 1"))
Message 7 of 9

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

Another using vl-string-search


An advantage of the ...search version over the ...position version is that you can look for a string of multiple characters [even multiple words], not limited to a single character.  An advantage of the ...position version is that it has the option to search from the end rather than from the beginning.  So if, for example, the OP wanted to find the part of a text string after the last space, rather than before the first one, that's pretty simple with the ...position function, but much more complicated if attempted with ...search, which would probably require pulling successively shortened substrings to search for the next space in, until there is no next space left.

Kent Cooper, AIA
0 Likes
Message 8 of 9

ronjonp
Mentor
Mentor

To get a string 'split' into different parts like the OP example, I'd use Lee's function.

@Kent1Cooper Definitely agree about the position search from end flag for grabbing the last item .. it's nice. 🙂

0 Likes
Message 9 of 9

Sea-Haven
Mentor
Mentor

Agree use Lee's function "This is a test" Do like though Kents simple code for 1st string.

 

 

 

(setq str "This is test")
(setq strlst (LM:str->lst str " "))
(setq 1ststring (car strlst))
or 
(setq lststring (nth 0 strlst))

car is 1st cadr is 2nd caddr is 3rd etc
or use nth where it starts item counter at 0 

 

 

 

Often you want more than just the 1st value.

 

 

;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] String to process
;; del - [str] Delimiter by which to separate the string
;; Returns: [lst] List of strings
 
(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

 

 

0 Likes