By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

String

P: n/a
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
Jul 24 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 24 Jul 2007 22:44:03 +0200, seema <se***@discussions.microsoft.comwrote:
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<chartable = 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]))
{
}

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 24 '07 #2

P: n/a
use String.ToCharArray

--
Sheng Jiang
Microsoft MVP in VC++
"seema" <se***@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
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

Jul 24 '07 #3

P: n/a
Sheng Jiang[MVP] <sh*********@hotmail.com.discusswrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 24 '07 #4

P: n/a
seema <se***@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 24 '07 #5

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
seema <se***@discussions.microsoft.comwrote:
>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.
The problem keeps getting more complicated, maybe time to reach for RegEx if
speed isn't important.
Jul 24 '07 #6

P: n/a
seema wrote:
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
Aug 21 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.