Questions tagged [char]

char refers to the character data-type, representing letters, digits, punctuation marks, control characters, etc. Use this tag for questions relating to and usage of the character data-type.

The keyword char is used in many programming languages, often defining a 1-byte memory location representing ASCII, UTF-8, UTF-16, UTF-32, and ISO-8895-1 character encodings. Arrays of characters can create Strings used to form words, descriptive text, or even extremely large numbers unable to be represented exactly by 32 or 64 bits.

13281 questions
3823
votes
17 answers

Why is char[] preferred over String for passwords?

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use String to handle passwords. Why does String pose a threat to…
Ahamed
  • 39,245
  • 13
  • 40
  • 68
1065
votes
11 answers

How to convert a std::string to const char* or char*

How can I convert an std::string to a char* or a const char*?
user37875
  • 13,904
  • 9
  • 37
  • 43
937
votes
13 answers

How to convert a char to a String?

I have a char and I need a String. How do I convert from one to the other?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
577
votes
14 answers

What is the difference between char s[] and char *s?

In C, one can use a string literal in a declaration like this: char s[] = "hello"; or like this: char *s = "hello"; So what is the difference? I want to know what actually happens in terms of storage duration, both at compile and run time.
user188276
539
votes
16 answers

What is an unsigned char?

In C/C++, what an unsigned char is used for? How is it different from a regular char?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
506
votes
8 answers

.NET / C# - Convert char[] to string

What is the proper way to turn a char[] into a string? The ToString() method from an array of characters doesn't do the trick.
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
467
votes
18 answers

std::string to char*

I want to convert a std::string into a char* or char[] data type. std::string str = "string"; char* chr = str; Results in: “error: cannot convert ‘std::string’ to ‘char’ ...”. What methods are there available to do this?
user1598585
446
votes
14 answers

How to convert a char array back to a string?

I have a char array: char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}; My current solution is to do String b = new String(a); But surely there is a better way of doing this?
chutsu
  • 13,612
  • 19
  • 65
  • 86
412
votes
5 answers

How can I convert a character to a integer in Python, and viceversa?

I want to get, given a character, its ASCII value. For example, for the character a, I want to get 97, and vice versa.
Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95
336
votes
12 answers

Delete last char of string

I am retrieving a lot of information in a list, linked to a database and I want to create a string of groups, for someone who is connected to the website. I use this to test but this is not dynamic, so it is really bad: string strgroupids = "6"; I…
Kiwimoisi
  • 4,086
  • 6
  • 33
  • 64
316
votes
23 answers

Why is there no Char.Empty like String.Empty?

Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings. Edit: The reason for this usage was this: myString.Replace ('c', '') So…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
296
votes
5 answers

How to convert a char array to a string?

Converting a C++ string to a char array is pretty straightorward using the c_str function of string and then doing strcpy. However, how to do the opposite? I have a char array like: char arr[ ] = "This is a test"; to be converted back to: string str…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
266
votes
4 answers

Why is conversion from string constant to 'char*' valid in C but invalid in C++

The C++11 Standard (ISO/IEC 14882:2011) says in § C.1.1: char* p = "abc"; // valid in C, invalid in C++ For the C++ it's OK as a pointer to a String Literal is harmful since any attempt to modify it leads to a crash. But why is it valid in C? The…
rullof
  • 7,124
  • 6
  • 27
  • 36
259
votes
15 answers

Single quotes vs. double quotes in C or C++

When should I use single quotes and double quotes in C or C++ programming?
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
253
votes
9 answers

How do I apply the for-each loop to every character in a String?

So I want to iterate for each character in a string. So I thought: for (char c : "xyz") but I get a compiler error: MyClass.java:20: foreach not applicable to expression type How can I do this?
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
1
2 3
99 100