Solved

Checking the first character in a string

Posted on 2004-10-11
8
212 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reference issues in Visual Studio 3 41
Cant save 3D 4 23
How to parse the string and split the sub directory names in asp.net 7 58
itextsharp with c# 3 9
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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