Solved

trim all but first letter in VB.net

Posted on 2012-03-12
6
382 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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