By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

substring

P: n/a
Hi

i have a string value that contains a number this could look like
03843282922132 now the 0 at the beginning is the important bit when using
substring to get every three charachters it misses off the first 0 is there
a way round this or another way of doing it, here is my currect code.

Dim m As String

m = TextBox3.Text

Dim x(m.Length) As Integer

For i As Integer = 0 To m.Length - 1

x(i) = m.Substring(i, 3)

Debug.WriteLine(x(i))

Next

Any help apreciated,

Mike.

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Dim m As String

m = TextBox1.Text

Dim x As String

For i As Integer = 0 To m.Length - 1

If m.Length - 1 - i > 3 Then

x = m.Substring(i, 3)

Else
x = m.Substring(i, m.Length - 1 - i)
End If

Debug.WriteLine(x)

Next

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Michael Turner" <fi*****@m-turner.co.uk> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
Hi

i have a string value that contains a number this could look like
03843282922132 now the 0 at the beginning is the important bit when using
substring to get every three charachters it misses off the first 0 is there a way round this or another way of doing it, here is my currect code.

Dim m As String

m = TextBox3.Text

Dim x(m.Length) As Integer

For i As Integer = 0 To m.Length - 1

x(i) = m.Substring(i, 3)

Debug.WriteLine(x(i))

Next

Any help apreciated,

Mike.

Nov 20 '05 #2

P: n/a
"Michael Turner" <fi*****@m-turner.co.uk> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
i have a string value that contains a number this could look like
03843282922132 now the 0 at the beginning is the important bit when using substring to get every three charachters it misses off the
first 0
It does no such thing! Follow your own code as it works its magic...
Dim m As String
m = TextBox3.Text
m now contains the String "03843282922132".
Dim x(m.Length) As Integer
NB: Integer *not* String!!
For i As Integer = 0 To m.Length - 1
x(i) = m.Substring(i, 3)
' e.g. x( 0 ) now holds the Integer 38
Debug.WriteLine(x(i))
Next


Each trio of digits is being "Evilly Type Coerced" from a String into
an Integer and, of course, Integers don't have leading zeroes - only
the String /representations/ of them do.
If you /want/ leading zeroes, keep them as Strings; if you want to
have numbers that you /display/ as Strings, do so, as in

For i As Integer = 0 To m.Length - 1
x( i ) = CInt( m.Substring( i, 3 ) )
Debug.WriteLine( x( i ).ToString( "000" ) )
Next

BTW, please, please *please*, put

Option Strict On

at the top of your source file (or set it in the default project settings).
I cannot recommend its use strongly enough.

It /will/ break a lot of your code but, by the time you've fixed them
all, you'll understand type conversion much, /much/ better.

HTH,
Phill W.
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.