String

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a string strA = "12 MyAddress"
strB = "Your Name"

I want to get the first char of string and want to know weather it is
integer between 1-9 or char between A-B.

How can I do that.

Thanks
 
Hi,

I have a string strA = "12 MyAddress"
strB = "Your Name"

I want to get the first char of string and want to know weather it is
integer between 1-9 or char between A-B.

How can I do that.

Thanks

Lot of different ways to do this, and one of them is by creating a lookup table with the specific values and compare the first character of eachstring against this table.

List<char> table = new List<char>(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B' });
string strA = "12 MyAddress";
string strB = "Your Name";

if(table.Contains(strA[0]))
{
}

if(table.Contains(strB[0]))
{
}
 
seema said:
I have a string strA = "12 MyAddress"
strB = "Your Name"

I want to get the first char of string and want to know weather it is
integer between 1-9 or char between A-B.

How can I do that.

Please read the replies to the same question you asked yesterday.
 
Sheng Jiang said:
use String.ToCharArray

What, just to get the first character? Rather wasteful when myString[0]
would do it without copying a whole load of unneeded data...
 
Jon Skeet said:
Please read the replies to the same question you asked yesterday.

The problem keeps getting more complicated, maybe time to reach for RegEx if
speed isn't important.
 
seema said:
Hi,

I have a string strA = "12 MyAddress"
strB = "Your Name"

I want to get the first char of string and want to know weather it is
integer between 1-9 or char between A-B.

How can I do that.

bool found = false;

if ((strA != null) && (strA.Length > 0))
{
char ch = strA [0];

found = (char.IsDigit (ch) && (ch != '0')) || (ch == 'A') || (ch == 'B');
}

There are numerous ways to optimize this especially if you know something
about the data, but as a start, this would be pretty good.

Hilton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top