Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am currently creating a text file

one of the fields i am reading from my db needs to be limited to 55 charaters if it exceeds the character limit it should move to the next line but keep the format

e.g

Date Event details id
2014/05/12 5555 SomeText(40Char) 1
2014/05/12 5565 sometext(55Char) 1
SomeText(55Char) 1
SomeText(30char) 1
2014/05/15 5597 SomeText(15char) 1
Posted
Comments
[no name] 15-May-14 10:31am    
Check the length of the string before you write it out to the file.

1 solution

Consider the following
VB
Dim aTest As String = "dasjfasldjflsdjfasjkldf;asldjflasjkdflasdjflasjdflssdfsadf"
Debug.Print(aTest)
Debug.Print(ChopString(aTest, 55))
aTest = "dasjfasldjflsdjfasjkldf;asldjflasj"
Debug.Print(ChopString(aTest, 55))
...

Private Function ChopString(ByVal inString As String, ByVal inLen As Integer) As String
    Return inString.Substring(0, IIf(inString.Length > inLen, inLen, inString.Length))
End Function
which produces the output
CSS
dasjfasldjflsdjfasjkldf;asldjflasjkdflasdjflasjdflssdfsadf
dasjfasldjflsdjfasjkldf;asldjflasjkdflasdjflasjdflssdfs
dasjfasldjflsdjfasjkldf;asldjflasj

In other words if aTest represents your input from your database the ChopString function will truncate it to 55 characters if it is longer than that, or return all of the string if it is 55 characters or shorter.
Just output the result to your text file instead of your db value
 
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