?
Solved

trim all but first letter in VB.net

Posted on 2012-03-12
6
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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