Solved

trim all but first letter in VB.net

Posted on 2012-03-12
6
377 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

18 Experts available now in Live!

Get 1:1 Help Now