Solved

Checking the first character in a string

Posted on 2004-10-11
8
208 Views
Last Modified: 2010-04-15
I have a string variable that I need to check the first character on.

How do I check if the variable var1 has a "-" for the first character?
0
Comment
Question by:RosanneParnell
8 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 12279538
if (var1.InString("-") == 0) {
 // do whatever
}
0
 

Author Comment

by:RosanneParnell
ID: 12279615
Will this work for only the first character or is this going to be true if there are other "-" in my string?
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12279634
Try this:

----------------------------------------
if( var.Substring(0,1) == "-" )
{
    .....
}
----------------------------------------

-tushar
0
 
LVL 14

Expert Comment

by:dfu23
ID: 12279695
I'm sorry, I meant to use IndexOf

so the code example would be:

if (var1.IndexOf("-") == 0) {
 // do whatever
}

This searches the string from left to right and will return the index of first instance of that character. So if it finds a dash "=" at the first index (index 0) the if statement will be true. If it finds it later in the string or not at all in the string this will not be true.

tusharashah's comment would work but would generate an error if the string was zero length ... ""
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Accepted Solution

by:
Timbo87 earned 50 total points
ID: 12279837
I've found that using a character based indexer yields almost twice the performance of Substring for extracting a single character.

if(var1[0] == '-')
     // first character is -
0
 
LVL 14

Expert Comment

by:dfu23
ID: 12280082
Once again ... if the string is zero length this will throw an error ... might want to put it in a try catch block.
0
 
LVL 9

Expert Comment

by:msdixon
ID: 12280197
dfu23

a try / catch block is very inefficient. it's better to check the length first.

if (var1.Length > 0)
{
   // check if it starts with a dash
   // perform your action here....
}


on a side note...
there's also the StartsWith method

if (var1.StartsWith("-"))
{
   // do stuff
}

i haven't done any performance tests... but sometimes, it's good to know multiple ways of doing the same thing.
0
 
LVL 14

Expert Comment

by:dfu23
ID: 12280344
Cool, I learned something new ... never knew there was a StartsWith method ...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

11 Experts available now in Live!

Get 1:1 Help Now