473,769 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2466
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.Visua lBasic, 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*******@disc ussions.microso ft.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.Visua lBasic, 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.co m>
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
2606
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 in the changes document? John Roth
0
1340
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", not actually of type T. Firstly, I can't seem to find an exact definition of "convertable to T". While I assume it is the obvious definition (an implicit conversion), I can't find an exact definition. Is there one?
9
1846
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
2129
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
7146
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 IsNumeric to do this, but how can i do it in C#? Thanks in advance, Daniel
4
2173
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 true. The function looks like this: #include <iostream> #include <algorithm> #include <string> using namespace std;
6
2206
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 and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
8
1934
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 the array m_columnBlowStep is string because I have used ToString on each enum item as you can see. I want to use enum in the case statment in the switch. I know I can use string but I rather want to use enum.
2
719
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 is in the editable mode). This is the code: ----------------------------------- Dim strQty As String Dim iQty As Integer strQty = CType(ea.Item.Cells(5).Controls(0), TextBox).Text
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.