Click here to Skip to main content
13,633,209 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I want to divide a string into three parts. I am using following code.
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 12-Jan-18 16:44pm
Updated 14-Jan-18 8:17am
v3
Comments
12-Jan-18 23:58pm
   
How can i do this in visual basic 6 ?
13-Jan-18 2:40am
   
i have completed all my project except this topic.please help me
13-Jan-18 3:30am
   
i want to do this with Mid$ function not only Split$
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

//
// 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...

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)
  Permalink  
Comments
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[^]
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.
13-Jan-18 3:30am
   
i want to do this with Mid$ function not only Split$
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
Graeme_Grant 13-Jan-18 5:24am
   
mid will do it. Just calc the break points...
Dim width = Len(Text1.text) / 3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

As Greeme_Grant[^] mentioned in the comment to your question, Mid function will do that. Check this:

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180712.1 | Last Updated 14 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100