Solved

trim all but first letter in VB.net

Posted on 2012-03-12
6
379 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…

823 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