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
251
votes
10 answers

Java: parse int value from a char

I just want to know if there's a better solution to parse a number from a character in a string (assuming that we know that the character at index n is a number). String element = "el5"; String s; s = ""+element.charAt(2); int x =…
Noya
  • 3,879
  • 3
  • 26
  • 32
235
votes
14 answers

How to convert/parse from String to char in java?

How do I parse a String value to a char type, in Java? I know how to do it to int and double (for example Integer.parseInt("123")). Is there a class for Strings and Chars?
Ren
  • 4,594
  • 9
  • 33
  • 61
227
votes
20 answers

Convert char to int in C#

I have a char in c#: char foo = '2'; Now I want to get the 2 into an int. I find that Convert.ToInt32 returns the actual decimal value of the char and not the number 2. The following will work: int bar = Convert.ToInt32(new string(foo,…
KeithA
  • 2,525
  • 3
  • 18
  • 15
218
votes
15 answers

Which letter of the English alphabet takes up most pixels?

I am trying to do some dynamic programming based on the number of characters in a sentence. Which letter of the English alphabet takes up the most pixels on the screen?
keruilin
  • 16,782
  • 34
  • 108
  • 175
209
votes
8 answers

Returning an array using C

I am relatively new to C and I need some help with methods dealing with arrays. Coming from Java programming, I am used to being able to say int [] method() in order to return an array. However, I have found out that with C you have to use pointers…
user1506919
  • 2,377
  • 5
  • 20
  • 15
203
votes
6 answers

Is char signed or unsigned by default?

In the book "Complete Reference of C" it is mentioned that char is by default unsigned. But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default. Which one is correct?
C Learner
  • 2,287
  • 2
  • 14
  • 7
198
votes
4 answers

How to sort with a lambda?

sort(mMyClassVector.begin(), mMyClassVector.end(), [](const MyClass & a, const MyClass & b) { return a.mProperty > b.mProperty; }); I'd like to use a lambda function to sort custom classes in place of binding an instance method. However,…
BTR
  • 4,880
  • 4
  • 24
  • 21
196
votes
9 answers

What is the best way to tell if a character is a letter or number in Java without using regexes?

What is the best and/or easiest way to recognize if a string.charAt(index) is an A-z letter or a number in Java without using regular expressions? Thanks.
Daniel Sopel
  • 3,535
  • 4
  • 24
  • 17
189
votes
22 answers

How to get the first five character of a String

I have read this question to get first char of the string. Is there a way to get the first n number of characters from a string in C#?
Knight Wing
  • 1,919
  • 2
  • 11
  • 6
186
votes
9 answers

Fastest way to iterate over all the chars in a String

In Java, what would the fastest way to iterate over all the chars in a String, this: String str = "a really, really long string"; for (int i = 0, n = str.length(); i < n; i++) { char c = str.charAt(i); } Or this: char[] chars =…
Óscar López
  • 232,561
  • 37
  • 312
  • 386
180
votes
5 answers

Get a substring of a char*

For example, I have this char *buff = "this is a test string"; and want to get "test". How can I do that?
user502230
174
votes
9 answers

How can I create a string from a single character?

I have a single char value, and I need to cast/convert it to a std::string. How can I do this? I know how to go the opposite way, to retrieve a char from a std::string object: you just need to index the string at the appropriate location. For…
weeo
  • 2,619
  • 5
  • 20
  • 29
171
votes
19 answers

Convert int to char in java

Below is a code snippet, int a = 1; char b = (char) a; System.out.println(b); But what I get is empty output. int a = '1'; char b = (char) a; System.out.println(b); I will get 1 as my output. Can somebody explain this? And if I want to convert an…
user2640480
  • 1,899
  • 2
  • 13
  • 8
152
votes
6 answers

C char array initialization: what happens if there are less characters in the string literal than the array size?

I'm not sure what will be in the char array after initialization in the following ways. 1.char buf[10] = ""; 2. char buf[10] = " "; 3. char buf[10] = "a"; For case 2, I think buf[0] should be ' ', buf[1] should be '\0', and from buf[2] to…
lkkeepmoving
  • 2,323
  • 5
  • 25
  • 31
150
votes
1 answer

How can I use modulo operator (%) in JavaScript?

How can I use modulo operator (%) in calculation of numbers for JavaScript projects?
Ziyaddin Sadigov
  • 8,893
  • 13
  • 34
  • 41