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

Convert a String to a char...

Hi

There is any easy way to convert a 1Char string to a char variable
string str="A"
char ch=str..

Thanks for your help

Nov 16 '05 #1
7 3144
string str ="A";

char ch = str[0];
-----Original Message-----
Hi,

There is any easy way to convert a 1Char string to a char variable?string str="A";
char ch=str...

Thanks for your help,

.

Nov 16 '05 #2

"Bernardo" wrote...
There is any easy way to convert a 1Char string to a char variable?
string str="A";
char ch=str...


This gets the first character in the string:

char ch = str[0];

// Bjorn A
Nov 16 '05 #3
Hi Bernardo,
"Bernardo" <be*******************@eds.com> schrieb im Newsbeitrag
news:8B**********************************@microsof t.com...
Hi,

There is any easy way to convert a 1Char string to a char variable?
string str="A";
char ch=str...

Thanks for your help,


try:
string str = "A";

char ch = Convert.ToChar(str);

HTH,
Marc
Nov 16 '05 #4
remember that string is an array of char, therefore you can access each
element by its index.

e.g.

string s = "abc";

char ch = s[0];
--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Bernardo" <be*******************@eds.com> wrote in message
news:8B**********************************@microsof t.com...
Hi,

There is any easy way to convert a 1Char string to a char variable?
string str="A";
char ch=str...

Thanks for your help,

Nov 16 '05 #5
bear in mind Bernardo that if you try the conversion using Convert and the
string is greater than 1 char or null then an exception will be thrown,
likewise if you try accessing the 0 index of the string and the string is
empty or null then an exception will also be thrown.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Marc Lörner" <ea*********@gmx.net> wrote in message
news:Op**************@TK2MSFTNGP12.phx.gbl...
Hi Bernardo,
"Bernardo" <be*******************@eds.com> schrieb im Newsbeitrag
news:8B**********************************@microsof t.com...
Hi,

There is any easy way to convert a 1Char string to a char variable?
string str="A";
char ch=str...

Thanks for your help,


try:
string str = "A";

char ch = Convert.ToChar(str);

HTH,
Marc

Nov 16 '05 #6
Mark Broadbent <no************@no-spam-please.com> wrote:
remember that string is an array of char, therefore you can access each
element by its index.

e.g.

string s = "abc";

char ch = s[0];


No, a string isn't actually an array of chars. However, it has an
indexer which returns the character at the appropriate position.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
thx for correction. had read that one time but obviously hadnt sunk in (it
also explains the immutable aspect of strings).

cheers.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mark Broadbent <no************@no-spam-please.com> wrote:
remember that string is an array of char, therefore you can access each
element by its index.

e.g.

string s = "abc";

char ch = s[0];


No, a string isn't actually an array of chars. However, it has an
indexer which returns the character at the appropriate position.

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

Nov 16 '05 #8

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

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
2
by: Steve Kershaw | last post by:
I can convert from a string to a char array (char) by using string.ToCharArray() function. Now how do I convert it back? The string looks like this: "000457". I've converted it to a char array:...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.