substring

M

Michael Turner

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

One Handed Man \( OHM - Terry Burns \)

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
 
P

Phill. W

Michael Turner said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top