Solved

Checking the first character in a string

Posted on 2004-10-11
8
209 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
fomat Json objects 6 28
C# application error "Parameter Is Not Valid". 3 80
Angular JS Route 3 54
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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 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

21 Experts available now in Live!

Get 1:1 Help Now