473,326 Members | 2,168 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,326 software developers and data experts.

C# Newby String to Char Conversion question

Hi:

I'm more than a little confused with this question:

I need to create a char type from the first letter in a string - for
example:

string mystring;
char mychar;

mychar = (first character of mystring)

I have looked all around and find many examples of character arrays however
I can't find a simple example of how to do this - probably because it's
simple - at least I hope.

Any help with this woudl be GREATLY appreciated!

Thansk,

Fred
Nov 17 '05 #1
7 30371
Hi Fred,

You can access each character in a string by using its indexer

char myChar = myString[index];

myString[0] would then be the first character in the string.

On Wed, 18 May 2005 14:33:16 +0200, Fred Nelson <fr**@smartybird.com> wrote:
Hi:

I'm more than a little confused with this question:

I need to create a char type from the first letter in a string - for
example:

string mystring;
char mychar;

mychar = (first character of mystring)

I have looked all around and find many examples of character arrays however
I can't find a simple example of how to do this - probably because it's
simple - at least I hope.

Any help with this woudl be GREATLY appreciated!

Thansk,

Fred


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
Heh Fred

This works for me, and seems straightforward enough:

string mystring = "yada";
char mychar;
mychar = mystring.ToCharArray(0,1)[0];

Cheers
Bill

Nov 17 '05 #3
Actually, try this instead:
mychar = mystring[0]

Nov 17 '05 #4
Thanks Everyone!

Now I get it and I also understand the documentation that was showing how to
manipulate character arrays.

Fred

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl...
Hi:

I'm more than a little confused with this question:

I need to create a char type from the first letter in a string - for
example:

string mystring;
char mychar;

mychar = (first character of mystring)

I have looked all around and find many examples of character arrays however I can't find a simple example of how to do this - probably because it's
simple - at least I hope.

Any help with this woudl be GREATLY appreciated!

Thansk,

Fred

Nov 17 '05 #5

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks Everyone!

Now I get it and I also understand the documentation that was showing how
to
manipulate character arrays.


As posted by the previous posters
mychar = mystring[0]
works fine.

HOWEVER
if the string is "" //empty
You will throw an exception

Bill
Nov 17 '05 #6
Thanks Bill:

I have this within a try/catch!

Fred

"Bill Butler" <Bi@DigitalArts.c0m> wrote in message
news:uk*************@TK2MSFTNGP15.phx.gbl...

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks Everyone!

Now I get it and I also understand the documentation that was showing how to
manipulate character arrays.


As posted by the previous posters
mychar = mystring[0]
works fine.

HOWEVER
if the string is "" //empty
You will throw an exception

Bill

Nov 17 '05 #7
Fred Nelson <fr**@smartybird.com> wrote:
I have this within a try/catch!


It's not a good idea to catch exceptions which can very easily be
avoided. If it's valid for the string to be empty (or null), you should
probably special case that and avoid taking the first character of it.
If it's not valid for the string to be empty or null at that point, you
should check it and throw an exception yourself.

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

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

Similar topics

10
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type...
9
by: fooboo | last post by:
Does anyone know if a easier way (built in function, or something) that can verify that a string is an alphanumeric number? Here is what I am doing now: for(i=0; i < strlen(temp); i++){...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
5
by: news.chi.sbcglobal.net | last post by:
I have a question about string literals with C++/CLI. Is there a difference between the following two lines? String ^s1 = gcnew String("Hello"); String ^s2 = gcnew String(L"Hello"); I have...
11
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
5
by: alexrixhardson | last post by:
Hi guys, I am a newby in the C/C++ world, and I am beginning to work on a rather simple TCP/IP proxy application which must be able to handle large volume of data as quickly as possible. ...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.