Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I simulate val(mid(MyString,2,1))?

Status
Not open for further replies.

dlunday

Programmer
May 20, 2002
5
US
I think strong typing is gonna drive me nuts :)

I have a string, say MyString = "12345" and I want to convert the second character to an int. I VB it was easy: val(mid(MyString,2,1)) = 2

In C# its somewhat tougher, since I can't convert a string OR a char[] to an integer.

These don't work:

int i = MyString.Substring(2,1);
int i =MyString.ToCharArray(2,1);

Nor can I cast my way out of this:

int i = (char)MyString.Substring(2,1);
int i =(char)MyString.ToCharArray(2,1);

Am I missing something obvious, or have I simply butted heads with strong typing? What's the most elegant solution out of this? Overloading the string class?

 
Never mind. I answered my own question. I was looking at it backwards. Figured it should be in the String class somewhere. Instead its in the Decimal class.. this works fine.

Decimal dc = Decimal.Parse("123");

dc = 123

Ahh, the joys of programming :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top