473,805 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 30391
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**@smartybir d.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.ToChar Array(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**@smartybir d.com> wrote in message
news:uv******** ******@tk2msftn gp13.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**@smartybir d.com> wrote in message
news:%2******** ********@tk2msf tngp13.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******** *****@TK2MSFTNG P15.phx.gbl...

"Fred Nelson" <fr**@smartybir d.com> wrote in message
news:%2******** ********@tk2msf tngp13.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**@smartybir d.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.co m>
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
3638
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 of literal "foo" is char, which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows modification of "foo", which in my opinion should
9
17007
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++){ if(!isalnum(temp)){ return 1 } return 0;
9
2049
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 function I'm trying to write. I'm writing a function that takes a string, and returns an array of strings which are the result of splitting the input on whitespace and parentheses (but the parentheses should also be included in the array as...
12
2218
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 <string.h> are correct, is it possible that an error occurs, and what an error might this be? Regards
11
17657
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 s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
5
3031
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 been playing around with the compiler options for "Character Set" and "Common Language Runtime Support" and those two lines compile fine. I am thinking that when compiling with /clr, "Hello" is a string of Unicode
11
2718
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 receiving a char buffer into a string? Reason I ask is otherwise I have to guess at what size buffer to create? And then copy buffer to a string. Doesn't seem ideal.
5
1834
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. Since this application has to process and distribute plain text around the network, I am wondering if there are any peformance differences between C++ std::string and C char in run time?
8
4745
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 add the unique words in the string to a map. Eg:
0
10613
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
10368
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
9186
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...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
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
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.