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
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Solved by Sergio.D.Suárez. Go to 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
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
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()
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
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
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?
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
Can't find what you're looking for? Ask the community or share your knowledge.