473,387 Members | 3,750 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,387 software developers and data experts.

How to determine it is a string,int,double,char??

For example.
If i enter "100" in textbox then
messagebox will popup.. "Its a Integer".
or i enter "hello world" then
messagebox will popup "Its a String"..
Etc....
Sep 7 '11 #1

✓ answered by GaryTexmo

It's hard to outright say... there's some overlap there. Consider the data you enter into a text box is always a string. Any text can be considered a string. Also, some numbers are only doubles... and all floats are doubles, but not all doubles are floats. Same for integers.

So you'll need decide some kind of hierarchy, then just test. For this, you can use the TryParse on most built in value types. You'll just have to start testing, then use else-ifs to check each successive type. Something like this...

Expand|Select|Wrap|Line Numbers
  1. double d;
  2. float f;
  3. integer i;
  4.  
  5. if (double.TryParse(stringValue, out d))
  6.   // string is a double, value in d
  7. else if (float.TryParse(stringValue, out f))
  8.   // string is a float, value in f
  9. else if (int.TryParse(stringValue, out i))
  10.   // string is an int, value in i
  11. else
  12.   // string can't be converted, so it's just text
  13.  
Something like that.

2 3002
michaeldebruin
134 100+
string = text
int = numbers
double = numbers separated with a ,
char = one single character like a or c or 1 or 2
Sep 9 '11 #2
GaryTexmo
1,501 Expert 1GB
It's hard to outright say... there's some overlap there. Consider the data you enter into a text box is always a string. Any text can be considered a string. Also, some numbers are only doubles... and all floats are doubles, but not all doubles are floats. Same for integers.

So you'll need decide some kind of hierarchy, then just test. For this, you can use the TryParse on most built in value types. You'll just have to start testing, then use else-ifs to check each successive type. Something like this...

Expand|Select|Wrap|Line Numbers
  1. double d;
  2. float f;
  3. integer i;
  4.  
  5. if (double.TryParse(stringValue, out d))
  6.   // string is a double, value in d
  7. else if (float.TryParse(stringValue, out f))
  8.   // string is a float, value in f
  9. else if (int.TryParse(stringValue, out i))
  10.   // string is an int, value in i
  11. else
  12.   // string can't be converted, so it's just text
  13.  
Something like that.
Sep 9 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
6
by: karthik.naig | last post by:
Hi, This was the routine I wrote earlier to convert a C++ string to a char array. But I found that the char* array consisted only of junk after returning from the below function.
0
by: karthik.naig | last post by:
Hi, This was the routine I wrote earlier to convert a C++ string to a char array. But I found that the char* array consisted only of junk after returning from the below function. int...
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
20
by: Peteroid | last post by:
How the heck does one convert a String* to char? Specifically: String* my_string = "HELLO" ; char m_char_array ; m_char_array = my_string ; // error strcpy( m_char_array, my_string ) ; //...
8
by: Frank R Eid | last post by:
Hi all; A) What's the simplest way to convert a 'String' to 'char *' (and the other way araound) ? B) I have some C++ classes that I cannot easily convert to .NET (because they still are...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
5
oll3i
by: oll3i | last post by:
public class Cennik { private static Cennik instance = null; Map<String,Double> cennik = new HashMap<String,Double> (); private Cennik() { // prywatny konstruktor } public...
5
by: aarklon | last post by:
Hi all, why printf("....%d",sizeof((int)(double)(char) i)) always gives the size of int ??? is it because sizeof doesn't evaluate its operand....???
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.