Solved

vb.net count characters then add a symbol

Posted on 2004-09-24
7
327 Views
Last Modified: 2010-08-05
I am using visual basic and I want to know how I can count so many characters and then add a certain symbol

example that I have is:


L20Tom  Brady   12340golf

It needs to be when the code sees the line that starts with L to
make the line look lke this

L:20:Tom:Brady:12340:Golf

Now the line data will never be the same but it will have the same fixed length. The L will be the identifier to signal to do the above,

So
where 20 is would be 2 characters
Tom = 4 five characters
Brady = 10 characters
12340 = 5 chars
Golf= 4 chars

so lets say the next line would be

L40JimmyBrad    123  base

it would do this to the line

L:40:Jimmy:Brad:123:base

striping out the empty spaces.

Is this hard to do????


0
Comment
Question by:hogan9111
  • 4
  • 2
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12143548
do I understand you correctly that whent the FIRST CHARACTER is a 'L' the line will ALWAYS be laid out as

1 Character
2 DIGITS
5 Characters (may have trailing blanks)
10 Characters (may have trailing blanks)
5 Digits
4 characters

Is that correct?

AW
0
 

Author Comment

by:hogan9111
ID: 12143603
yes the L will never change

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12143721
and are the other character counts ALWAYS the same

2 characters ('20' in your example) followed by
5 Characters ('Tom  ' or 'Jimmy' in you examples) foloowed by
10 Characters ('Brady     ' or 'Brad      ' in your examples) followed by
5 Characters ('12340' in your example) followed by
4 characteres ('golf' or 'base' in your examples)

AW
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 12143837
If that is the case, then use can use the SubString Method of the String Object, like this:

Dim strTest As String = "L20Tom  Brady     12340golf"
Dim str(5) As String
Dim StrResult As String

str(0) = strTest.Substring(0, 1)
str(1) = strTest.Substring(1, 2)
str(2) = strTest.Substring(3, 5).Trim
str(3) = strTest.Substring(8, 10).Trim
str(4) = strTest.Substring(18, 5)
str(5) = strTest.Substring(23, 1).ToUpper & strTest.Substring(24, 3)

For i As Integer = 0 To 5
    StrResult = StrResult & str(i) & ":"
Next
    StrResult = StrResult.Substring(0, StrResult.Length - 1)


AW
0
 
LVL 2

Expert Comment

by:Shahankit
ID: 12143988
Dim mainLine as string = "L40JimmyBrad    123  base "

Dim temp As String
Dim temp1 As String
Dim temp2 As String
Dim temp3 As String
Dim temp4 As String
Dim temp5 As String

temp = mainLine.Substring(0, 1)
temp1 = mainLine.Substring(2, 4)
temp2 = mainLine.Substring(5, 10)
temp3 = mainLine.Substring(11, 21)
temp4 = mainLine.Substring(22, 27)
temp5 = mainLine.Substring(28, 32)


Dim position1 As Int16 = temp1.IndexOf(" ")
Dim position2 As Int16 = temp2.IndexOf(" ")
Dim position3 As Int16 = temp3.IndexOf(" ")
Dim position4 As Int16 = temp4.IndexOf(" ")
Dim position5 As Int16 = temp5.IndexOf(" ")

If position1> 0 Then
temp1 = temp1.substring(0,position1)
End If
If position2> 0 Then
temp2 = temp2.substring(0,position2)
End If
If position3> 0 Then
temp3 = temp3.substring(0,position3)
End If
If position4> 0 Then
temp4 = temp4.substring(0,position4)
End If
If position5> 0 Then
temp5 = temp5.substring(0,position5)
End If

Dim resultline as string = temp + ":" + temp1 + ":" + temp2 + ":" + temp3 + ":" + temp4 + ":" + temp5


It might not be the best way to solve this problem... but still this will give you resulting string as you want

Best of luck
Ankit
0
 

Author Comment

by:hogan9111
ID: 12144643
one question though: what is this line actually doing??

 StrResult = StrResult.Substring(0, StrResult.Length - 1)
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12144953
in the loop, I append a ":" after each substring, so this last statement drops the ":" which would have appeared after the LAST block.  It takes the Substring the starts at the first character and extends to the next to last character (Length - 1)

AW
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hasOne  challenge 59 92
userCompare  challenge 3 62
groovy example issue 10 70
Program to display an alert on Windows Toolbar 2 65
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now