I’m struggling with a piece of code and getting the error:
Too many characters in character literal error
Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line:
case ‘&&’:
case ‘||’:
case ‘==’:
How can I keep the == and && as a char?
asked Apr 9, 2011 at 17:37
1
This is because, in C#, single quotes ('') denote (or encapsulate) a single character, whereas double quotes ("") are used for a string of characters. For example:
var myChar = '=';
var myString = "==";
answered Apr 9, 2011 at 17:40
Grant ThomasGrant Thomas
44.3k10 gold badges85 silver badges129 bronze badges
0
Here’s an example:
char myChar = '|';
string myString = "||";
Chars are delimited by single quotes, and strings by double quotes.
The good news is C# switch statements work with strings!
switch (mytoken)
{
case "==":
//Something here.
break;
default:
//Handle when no token is found.
break;
}
answered Apr 9, 2011 at 17:50
You cannot treat == or || as chars, since they are not chars, but a sequence of chars.
You could make your switch…case work on strings instead.
answered Apr 9, 2011 at 17:39
driisdriis
161k45 gold badges265 silver badges341 bronze badges
A char can hold a single character only, a character literal is a single character in single quote, i.e. '&' — if you have more characters than one you want to use a string, for that you have to use double quotes:
case "&&":
answered Apr 9, 2011 at 17:39
BrokenGlassBrokenGlass
158k28 gold badges284 silver badges335 bronze badges
I faced the same issue.
String.Replace('\.','') is not valid statement and throws the same error.
Thanks to C# we can use double quotes instead of single quotes and following works
String.Replace("\.","")
answered Oct 6, 2020 at 14:28
I believe you can do this using a Unicode encoding, but I doubt this is what you really want.
The == is the unicode value 2A76 so I belive you can do this:
char c = 'u2A76';
I can’t test this at the moment but I’d be interested to know if that works for you.
You will need to dig around for the others. Here is a unicode table if you want to look:
http://www.tamasoft.co.jp/en/general-info/unicode.html
answered Apr 9, 2011 at 17:53
![]()
Abe MiesslerAbe Miessler
82.2k99 gold badges304 silver badges485 bronze badges
1
I’ beginner with C#. I was coding my first game and got error:Too many characters in character literal. How to fix it?
if (Input.GetAxisRaw('Horizontal') < 0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw('Horizontal' * moveSpeed * Time.deltaTime)));
}
![]()
Marc
3,9054 gold badges21 silver badges37 bronze badges
asked Feb 28, 2016 at 16:29
4
I see 2 things wrong.
First, Input.GetAxisRaw takes string as a parameter and string literals with used double quotes "" not single quotes ''. Single quotes are used for char type. That’s why you should it as;
Input.GetAxisRaw("Horizontal")
Second, that method returns float and if you do some calculations about that, the right syntax should be
Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime
not
Input.GetAxisRaw('Horizontal' * moveSpeed * Time.deltaTime))
answered Feb 28, 2016 at 16:34
Soner GönülSoner Gönül
96.8k102 gold badges205 silver badges362 bronze badges
3
You should use double quotes instead:
if (Input.GetAxisRaw("Horizontal") < 0.5f);
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime));
}
answered Feb 28, 2016 at 16:30
![]()
S.SpiekerS.Spieker
6,9258 gold badges44 silver badges50 bronze badges
|
Sashaa_i 0 / 0 / 1 Регистрация: 02.11.2014 Сообщений: 64 |
||||
|
1 |
||||
|
27.12.2015, 12:11. Показов 25069. Ответов 3 Метки нет (Все метки)
return ‘pervy method’; — в этой строке выдает ошибку CS1012 Too many characters in character literal. Помогите пожалуйста разобраться что к чему и как исправить.
0 |
|
Администратор
15668 / 12629 / 5003 Регистрация: 17.03.2014 Сообщений: 25,708 Записей в блоге: 1 |
|
|
27.12.2015, 13:40 |
2 |
|
РешениеSashaa_i, строки в C# должны быть в двойных кавычках. Одинарные кавычки используются для символов.
1 |
|
0 / 0 / 1 Регистрация: 02.11.2014 Сообщений: 64 |
|
|
27.12.2015, 13:58 [ТС] |
3 |
|
всё равно ошибки Миниатюры
0 |
|
0 / 0 / 1 Регистрация: 02.11.2014 Сообщений: 64 |
|
|
27.12.2015, 14:04 [ТС] |
4 |
|
а нет, все хорошо, получилось, спасибо)
0 |
The «Too many characters in character literal error» in C# occurs when a single-character literal exceeds the maximum limit of a character data type. This error can occur when a programmer attempts to assign a string value that is longer than one character to a char variable. In C#, char literals are defined using single quotes (‘), and a char data type can only store a single character. If a string value that is longer than one character is assigned to a char variable, this error will be thrown.
Method 1: Use the First Character of the String
To fix the «Too many characters in character literal error» in C#, you can use the first character of the string. Here’s how to do it:
- Declare a string variable with the value that causes the error:
string myString = "Hello World";
- Use the first character of the string by accessing it with the index operator:
char myChar = myString[0];
- Use the character variable instead of the string in your code:
Console.WriteLine(myChar);
This will print the first character of the string, «H», to the console.
You can also use this method to get any character in the string by changing the index in step 2. For example, to get the third character:
char myChar = myString[2];
This will give you the character «l».
In summary, to fix the «Too many characters in character literal error» in C#, you can use the first character of the string by accessing it with the index operator.
Method 2: Convert the String to a Char Array
To fix the «Too many characters in character literal error» in C#, you can convert the string to a char array. Here’s how you can do it in a few simple steps:
- Declare a string variable with the string value that’s causing the error:
string myString = "This is a string with too many characters";
- Convert the string to a char array using the ToCharArray() method:
char[] myCharArray = myString.ToCharArray();
- Access the individual characters in the char array using an index:
char firstChar = myCharArray[0];
char secondChar = myCharArray[1];
Here’s the complete code:
string myString = "This is a string with too many characters";
char[] myCharArray = myString.ToCharArray();
char firstChar = myCharArray[0];
char secondChar = myCharArray[1];
In this example, we declared a string variable called «myString» that contains the string with too many characters. We then converted the string to a char array using the ToCharArray() method and stored the result in a new variable called «myCharArray». Finally, we accessed the first and second characters in the char array using their respective indices.
By converting the string to a char array, we can access each individual character without encountering the «Too many characters in character literal error».
Method 3: Convert the String to a Char
To fix the «Too many characters in character literal error» in C#, you can convert the string to a char. Here’s how:
string str = "Hello World!";
char ch = str[0]; // Convert the first character of the string to a char
This code will convert the first character of the string «Hello World!» to a char.
If you want to convert a specific character in the string to a char, you can use the Substring method to extract the character and then convert it to a char. Here’s an example:
string str = "Hello World!";
char ch = str.Substring(6, 1)[0]; // Convert the character at index 6 to a char
This code will extract the character at index 6 (which is ‘W’) from the string «Hello World!» and convert it to a char.
You can also use the ToCharArray method to convert the entire string to a char array. Here’s an example:
string str = "Hello World!";
char[] charArray = str.ToCharArray(); // Convert the entire string to a char array
This code will convert the entire string «Hello World!» to a char array.
By converting the string to a char, you can avoid the «Too many characters in character literal error» in C#.
You get the following error when you try to print anything on the console. Below is the complete error message you get.
error CS1012: Too many characters in character literal
Below is the mistake you might have done printing a text on the console.
Console.WriteLine('Hello World!');
C# compiler was expecting a single character since you have used character literal syntax. But you have given more than 1 character.
If you are using a single quote, give a single character inside it else you will get the above error. If you want to print more than 1 character, use double-quotes.
Console.WriteLine("Hello World!");
Thank you All!!! Hope you find this useful.
You must log in to post a comment.

Сообщение было отмечено Sashaa_i как решение
