Truncate String Value

shomaimay

New member
Joined
May 19, 2010
Messages
2
Programming Experience
Beginner
Hi Good Day!

Need your help on how to truncate my string value to 17 chareacters only.

in my program, the acceptable number of characters for First Name is 17. Characters more than 17 should be truncated but it may cut words in the middle. I need to make the length max 17 characters, but, if it cuts a word, it should also remove this half-word completely.

Thanks.;)
 
Last edited:
Try:

VB.NET:
dim stringArray() as String
dim textBoxThatMustBeLessThan17 as String
string = Split(fullnamegoeshere)

for i as integer = 0 to stringArray.Count

if stringArray(i).Length <= 17
textBoxThatMustBeLessThan17.Text += stringArray(i)
next

Ok, so what are we doing:

Get a stringArray ready to use
The textbox is whatever field you wanted to fill in, that must be less than 17
Fill the stringArray up with the name - NOTE: Read up on what SPLIT does!! (google)

loop through the array, if the name is less than 17, then add it to the text box, otherwise dont.


NOTE: Currently, if someones name is:
KYLEALLBRIGHTPROGRAMSFORBANANAS Jones

It will ONLY add Jones, I'll leave it to you now to enhance the program, and work out why.


p.s I wrote this with no IDE, don't just c&p
 
Hi Good Day!

Need your help on how to truncate my string value to 17 chareacters only.

in my program, the acceptable number of characters for First Name is 17. Characters more than 17 should be truncated but it may cut words in the middle. I need to make the length max 17 characters, but, if it cuts a word, it should also remove this half-word completely.

Thanks.;)
I'd just use SubString on it to grab the first 17 chars if it's longer than 17:
VB.NET:
Dim MyString As String = "Some Really long value that might be longer than 17 characters..."
If MyString.Length > 17I Then MyString = MyString.SubString(0I, 17I)
 
Using SubString would be the way I go about it as well. Need to add some additional logic in there to make sure you're not cutting a word in half.

VB.NET:
        Dim MyString As String = "Joe Supercalifragilisticexpialidocious"
        If MyString.Length >= 17 Then
            If MyString.Substring(17, 1) = " " Then
                MyString = MyString.Substring(0, 17)
            Else
                MyString = MyString.Substring(0, 17)
                MyString = MyString.Substring(0, MyString.LastIndexOf(" "c))
            End If
        End If

I'm a little confused as to why there would be more than 1 word in a First Name field.
 
Using SubString would be the way I go about it as well. Need to add some additional logic in there to make sure you're not cutting a word in half.

VB.NET:
        Dim MyString As String = "Joe Supercalifragilisticexpialidocious"
        If MyString.Length >= 17 Then
            If MyString.Substring(17, 1) = " " Then
                MyString = MyString.Substring(0, 17)
            Else
                MyString = MyString.Substring(0, 17)
                MyString = MyString.Substring(0, MyString.LastIndexOf(" "c))
            End If
        End If

I'm a little confused as to why there would be more than 1 word in a First Name field.
I'm a little confused to why there's be a space in a first name field o_0
 
I'm a little confused as to why there would be more than 1 word in a First Name field.

I'm a little confused to why there's be a space in a first name field o_0
Double first names (or given names) are common, where the second part is not a middle name. This may be a culture-thing, I think in US there is only ever one first name where if more it is reckoned middle names that one must only include as a initials and never speak of? :p

I would go with Regex, for example:
VB.NET:
Dim first As String = "Jonathan 012345678"
Dim pattern As String = "[\w ]{1,17}(?= |$)"
first = Regex.Match(first, pattern).Value
That is 18 chars and matches up to (not including) first space, ie "Jonathan".
VB.NET:
first = "Jonathan 01234"
Same pattern would match this whole string, 14 chars and a space in there.
VB.NET:
Dim first As String = "Jonathan 01234 678"
If there were three parts to first name (!?) like above that pattern would here match only the first two parts, ie same resultat as the second example.

Check this place for more regex info: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
 
Thanks

Thank so much for all your help. I learned a lot and my code is now working. ;) By the way, in our country there are a lot of First Names..;):rolleyes:;)
 
Back
Top