473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.