Ilogic split a string

Ilogic split a string

Anonymous
Not applicable
5,200 Views
6 Replies
Message 1 of 7

Ilogic split a string

Anonymous
Not applicable

Goodday,


I am searching for a way to split strings. Where:

Dim Separators() As Char = {";"} 

Doesnt seem to work at my code.

 

As you see in the code the separator is-> ;

for example, if the string is 1;2, it must be separated into 1 and 2 given to 2 parameters,

so spot1 -> "1" and spot2 -> "2"

This also applies to, for example, 4;6;13 in 4, 6 and 13 given to 3 parameters, etc.

 

I hope i can find the solution im looking for over here!

Kind Regards,

Thomas

Accepted solutions (2)
5,201 Views
6 Replies
Replies (6)
Message 2 of 7

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

 

Hi Thomas, this should be the snippet you use to split the string.

 

Dim Separators() As Char = {";"} 
Sentence = "1;2;3;4;5"
Words = Sentence.Split(Separators)
i = 0
For Each wrd In Words
MessageBox.Show("Word Index #" & i & " = " & Words(i))
i=i+1
Next

and for your specific case you could use

 

Dim Separators() As Char = {";"} 
Sentence = "10;20;3;4;5"
Words = Sentence.Split(Separators)

spot1 = Words(0)
spot2 = Words(1)

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

 

Where spot1 and spot2 are your user parameters to redefine.
I hope this helps solve your problem. regards

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 7

Anonymous
Not applicable

Thank you Sergio!

 

It worked did work, i changed it a bit to this;

Dim Separators() As Char = {";"} 
Sentence = StringParameter
If Sentence = "1;1" Then
Words = Sentence.Split(Separators)
spot1 = Words(0)
spot2 = Words(1)
Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True
Else If Sentence = "1;5;7" Then
Words = Sentence.Split(Separators)
spot1 = Words(0)
spot2 = Words(1)
spot3 = Words(2)
Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True
End If 
InventorVb.DocumentUpdate()
0 Likes
Message 4 of 7

Sergio.D.Suárez
Mentor
Mentor

I'm glad you solved it. The fragment is just an update

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

 so I usually place it at the end of the code, it is not necessary to repeat it many times, you can eliminate those that are higher and leave only the last one. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 5 of 7

LSKELLY
Enthusiast
Enthusiast

Hi Sergio

 

Thanks. This code has worked well for me but I find only one frustrating quirk with it. If I try to split on line breaks it splits it so that each split segment is preceded by a line break rather than deleting the splitting character as it did with the semi-colons. The modified code below will illustrate the problem:

 

Dim Separators() As Char = {";",vbCrLf} Sentence = "1" & vbCrLf & "2" & vbCrLf & "3" & vbCrLf & "4" & vbCrLf & "5"
Words = Sentence.Split(Separators)
i = 0
For Each wrd In Words
MessageBox.Show("Word Index #" & i & " = " & Words(i))
i=i+1
Next

 

Do you have any idea how to elegantly work around this or prevent it?

0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @LSKELLY 

 

I think this should work for you:

 

Sentence = "1" & vbCrLf & "2" & vbCrLf & "3" & vbCrLf & "4" & vbCrLf & "5"
Words = Sentence.Split(vbCrLf)

For Each wrd In Words
	'strip out carraige returns & linefeeds
	wrd = wrd.Replace(vbCr, "").Replace(vbLf, "")
	MessageBox.Show("Word Index #" & i & " = " & wrd)
Next

 

 

Also just as a reminder, you can search and ask programming questions of this type on the Inventor Customization forum:
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120

 

(I've asked the moderators that this topic be moved to that forum to help in the future)

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 7 of 7

LSKELLY
Enthusiast
Enthusiast

Excellent! Thanks Curtis!

 

Kicking myself right now.

0 Likes