Solved

trim all but first letter in VB.net

Posted on 2012-03-12
6
378 Views
Last Modified: 2012-03-13
I am trying to get the first letter of a first name
What I have is
            Dim trimString = txtFirstName.Text
            Dim stringLength = trimString.Length - 1
            Dim Initial = trimString.Remove(Right, stringLength)
            MessageBox.Show(Initial.ToString())

any help is appreciated
0
Comment
Question by:r3nder
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:rowansmith
ID: 37713188
MessageBox.Show (Left(txtFirstName.Text,1))
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37713231
The Left command is the Visual Basic way of doing things and might be awkward to use in a Form where it enters in conflict with the Left property of the form.

It might be better to do it the .NET way, by using the Substring method of the String class. It can be used without interference in a module as well as in a Form, uses an object oriented way of doing things by calling a method of the class instead of calling a command that acts on a value, and is portable between all the .NET languages:

MessageBox.Show(txtFirstName.Text.Substring(0, 1))

Substring plays the role of the 3 following commands in VB : Left, Right and Mid.
0
 
LVL 6

Author Comment

by:r3nder
ID: 37713252
I solved it using
            Dim trimString As String
            Dim initial As Char
            trimString = txtFirstName.Text
            initial = trimString.Chars(0)

now I can use it to create a username
Thanks anyway
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37713371
Do not close a question without allowing a good answer when you received one.

You question was showing the result in a MessageBox, so this is what we gave you.

Simply change our answers for something such as

initial = txtFirstName.Text.Substring(0, 1)

And if you prefer your way of doing things, then do not lose your time creating a variable for nothing. The Text property can be used just as a variable.

initial = txtFirstName.Text.Chars(0)

And if you really wanted to Trim, you could do the following:

initial = txtFirstName.Text.Trim.Chars(0)
0
 
LVL 11

Expert Comment

by:rowansmith
ID: 37713384
I concur with JamesBurger.  The question asked was for the first letter of a string.  We each showed valid ways to retrieve that first letter.

While your answer is also valid and I am certain others could come up with valid answers which are different again.

Please assign points.

Thanks.
0
 
LVL 11

Expert Comment

by:rowansmith
ID: 37713465
Thanks muchly r3nder :-)

This is what makes Experts-Exchange a community.

Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

947 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

23 Experts available now in Live!

Get 1:1 Help Now