Click here to Skip to main content
15,921,250 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to divide a string into three parts. I am using following code.
VB
dim length1 as string
dim length2 as string
dim lenght3 as string
length1=Mid$(Text1.text,1,30)
length2=Mid$(Text1.text,30,70)
length3=Mid$(Text1.text,70,100)
msgbox length1
msgbox lenght2
msgbox length3


msgbox 2 show me the length of 11,30. Why?

What I have tried:

What have wrong with my code? i know that Mid$ start at left of the string.
Posted
Updated 14-Jan-18 8:17am
v3
Comments
Member 13563162 12-Jan-18 23:58pm    
How can i do this in visual basic 6 ?
Member 13563162 13-Jan-18 2:40am    
i have completed all my project except this topic.please help me
Member 13563162 13-Jan-18 3:30am    
i want to do this with Mid$ function not only Split$
Member 13563162 13-Jan-18 5:10am    
Suppose text1 contain 100 characters. I want that msgbox 1 show 1 to 30 characters , msgbox 2 show next 31 to 70 characters and msgbox 3 show further 71 to 100 Characters.Thanks

C#
//
// Summary:
//     Retrieves a substring from this instance. The substring starts at a specified
//     character position and has a specified length.
//
// Parameters:
//   startIndex:
//     The zero-based starting character position of a substring in this instance.
//
//   length:
//     The number of characters in the substring.
//
// Returns:
//     A string that is equivalent to the substring of length length that begins at
//     startIndex in this instance, or System.String.Empty if startIndex is equal to
//     the length of this instance and length is zero.
//
// Exceptions:
//   T:System.ArgumentOutOfRangeException:
//     startIndex plus length indicates a position not within this instance.-or- startIndex
//     or length is less than zero.
[SecuritySafeCritical]
public String Substring(int startIndex, int length);
//
// Summary:
//     Retrieves a substring from this instance. The substring starts at a specified
//     character position and continues to the end of the string.
//
// Parameters:
//   startIndex:
//     The zero-based starting character position of a substring in this instance.
//
// Returns:
//     A string that is equivalent to the substring that begins at startIndex in this
//     instance, or System.String.Empty if startIndex is equal to the length of this
//     instance.
//
// Exceptions:
//   T:System.ArgumentOutOfRangeException:
//     startIndex is less than zero or greater than the length of this instance.
public String Substring(int startIndex);


So...

VB
Dim length1 as string
Dim length2 as string
Dim lenght3 as string
length1 = Text1.text.Substring(0, 9)
length2 = Text1.text.Substring(10, 19)
length3 = Text1.text.Substring(20, 29)
 
Share this answer
 
Comments
Member 13563162 12-Jan-18 23:54pm    
how can i do this in visual bsaic6 ?
Graeme_Grant 13-Jan-18 0:11am    
VB6 is dead, so assumed VB.Net ... here: vb6 split string - Google Search[^]
Member 13563162 13-Jan-18 0:21am    
i have completed all my project except this topic.please help me
Graeme_Grant 13-Jan-18 2:40am    
The link in my last comment should have your answer.
Member 13563162 13-Jan-18 3:30am    
i want to do this with Mid$ function not only Split$
As Greeme_Grant[^] mentioned in the comment to your question, Mid function will do that. Check this:

VB
Sub Test()
Dim myText As String
Dim tlength As Integer
Dim divider As Integer
Dim counter As Integer

myText = "Begin. Very long string. Lorem ipsusm. Lorem ipsum. Lorem ipsum. Very long string. End."
tlength = Len(myText)
divider = tlength / 3
counter = 1

Do While counter <= tlength
    'if counter + divider > tlength then
    MsgBox Mid(myText, counter, divider)
    counter = counter + divider
Loop

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900