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

Check, if string is convertable to another type

Hello,

maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?

Thanks in advance,
Daniel
Nov 15 '05 #1
2 2448
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.

-Philip

"Daniel" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hello,

maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?

Thanks in advance,
Daniel

Nov 15 '05 #2
Philip Rieck <st***@mckraken.com> wrote:
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.


If you're interested in performance and will be getting a lot of "bad
data", you should also consider a first-pass method which might (say)
look at each character in the string to make sure it's appropriate.
I've found that this kind of thing can be far faster at filtering out a
lot of obviously bad candidates and then using an "industrial
strength" technique on the rest than just going straight to the heavy
stuff.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
0
by: chris | last post by:
Hello, I have been reading the c++ standard recently, in particular the part about input iterators. I note that given an input iterator a for type T, then *a only has to be "convertable to T",...
9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
2
by: Daniel | last post by:
Hello, This is maybe a simple one: Is there any way to check, if a string can be converted to another type (like DateTime or Integer) without using try/catch? In VB, i can use IsDate or...
4
by: Gaijinco | last post by:
I made this function which given two strings c1 and c2 test if with the letters of both you can exactly form the string c3, so for exaple c1 = "evol" c2= "ution" and c3 = "evolution" will yield...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
2
by: rn5a | last post by:
A sub-routine retrieves the value in a TextBox (which ought to be an integer) which in turn resides in a DataGrid (actually the column is a BoundColumn which changes to a TextBox when the DataGrid...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.