473,320 Members | 1,965 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,320 software developers and data experts.

strings

Hi,

I have a following strings
strA = " 1 my first work out".
strB = "2 my second work out"
strC = " My last work out"

My strA and strB first char is int and strB first char is " ". I want to
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not.

Thanks
Jul 23 '07 #1
6 1874
Actually, the first character of strA and strC is a space, the first
character of strB is the character '2', not the actual integer to (it is the
character representation of 2 from the perspective of the framework).

Basically, to do what you want, I would split the string into an array
of strings using space as the delimiter (I'm assuming that's what you are
using to delimit words) and then seeing if the first element in the array is
an integer representation:

public static bool StartsWithInt(string value, out int intValue)
{
// Split the string.
string[] parts = value.Split(new char[]{ ' ' });

// If there are elements, then continue, otherwise, return
// false.
if (parts.Length = 0)
{
// No integer here.
return false;
}

// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null, which
you might want to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"seema" <se***@discussions.microsoft.comwrote in message
news:60**********************************@microsof t.com...
Hi,

I have a following strings
strA = " 1 my first work out".
strB = "2 my second work out"
strC = " My last work out"

My strA and strB first char is int and strB first char is " ". I want to
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not.

Thanks


Jul 23 '07 #2
On Mon, 23 Jul 2007 12:54:04 -0700, seema
<se***@discussions.microsoft.comwrote:
[...]
My strA and strB first char is int and strB first char is " ". I want to
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not.
The "first char" of a string is always a char. It is never an int.

However, you seem to really just want to find out whether that character
can be _converted_ to an int. IMHO, the easiest is to try to parse it as
an int:

int num;

if (int.TryParse(str.Substring(0, 1), out num))
{
// first character can be parsed as an int, and the value is
// in the variable num
}

Pete
Jul 23 '07 #3
On Mon, 23 Jul 2007 21:54:04 +0200, seema <se***@discussions.microsoft.comwrote:
Hi,

I have a following strings
strA = " 1 my first work out".
strB = "2 my second work out"
strC = " My last work out"

My strA and strB first char is int and strB first char is " ". I wantto
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not..

Thanks
Or, if you can ignore whitespace characters at the beginning, do

if (Char.IsDigit(strA.TrimStart()[0]))
{
// number
}

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 23 '07 #4
On Mon, 23 Jul 2007 13:05:10 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
[...]
// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null,
which
you might want to do.
It also does not limit the attempt at conversion to a single character.

Based on a literal reading of the OP, only the first character should be
used to attempt to convert to an int.

It is often the case that someone writes a question that is actually not
exactly what they really want to do, so your solution may in fact be the
correct one. But as stated, it's not doing what the OP asked for.

Pete
Jul 23 '07 #5
It is this that makes me think that the first character might not be
exactly what they are looking for:

I want to check if in a string first char returns int (1, 2 or ---)

"---" is ambiguous, and more than one character. An ellipsis would have
been better, but saying 1-9 would have been best.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 23 Jul 2007 13:05:10 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
>[...]
// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null,
which
you might want to do.

It also does not limit the attempt at conversion to a single character.

Based on a literal reading of the OP, only the first character should be
used to attempt to convert to an int.

It is often the case that someone writes a question that is actually not
exactly what they really want to do, so your solution may in fact be the
correct one. But as stated, it's not doing what the OP asked for.

Pete
Jul 24 '07 #6
On Mon, 23 Jul 2007 17:41:30 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
It is this that makes me think that the first character might not be
exactly what they are looking for:

I want to check if in a string first char returns int (1, 2 or ---)

"---" is ambiguous, and more than one character. An ellipsis would
have been better, but saying 1-9 would have been best.
Yup...a valid interpretation IMHO. Sadly, we are often left trying to
extract some consistent interpretation from an internally-inconsistent
post. :) I just wanted to clarify that there are some differences from
the explicit statements (as opposed to my own suggestion which has
differences from the implicit statements).

Pete
Jul 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.