+ 8

How do I make spaces invisible in Console.ReadLine?

I am making a program, and I want the spaces invisible for the iPad to read (for example, I want the same outcome between "A B C", and "ABC"). Please help me figure out how to do this.

4th Apr 2017, 12:34 AM
Luke Hegelund
Luke Hegelund - avatar
16 Answers
+ 25
string input = Console.ReadLine().Replace(" ",""); This is much cleaner, than the others.
11th Apr 2017, 3:33 PM
Ákos Nagy
Ákos Nagy - avatar
+ 23
Replace blank spaces with nothing so that blank spaces are removed. Here's how you can do it. str.Replace(' ', ''); // 'str' is your string.
4th Apr 2017, 12:51 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 13
You can use String.Replace. For example: string text = “A B C“; string rep = text.Replace(' ',''); //output ABC
4th Apr 2017, 12:41 AM
Max_N
Max_N - avatar
+ 12
@Andrew In C# the .Replace(); will replace the first argument with the second one, all in the string. Doesn't matter how much, for example, 'Space' are there.
13th Apr 2017, 3:14 PM
Ákos Nagy
Ákos Nagy - avatar
+ 5
The best answers have: str = str.Replace(' ',''); as solution, but in fact, the second argument ('') is not valid expression, because ' marks enclose a character, and if there is no character between the signs, it cannot be interpreted. You should use: str = str.Replace(" ", ""); as mentioned in other answers. Or in full code: string input = Console.ReadLine().Replace(" ", "");
13th Apr 2017, 4:26 PM
Magyar DĂĄvid
Magyar DĂĄvid - avatar
+ 2
Thanks, everyone!
11th Apr 2017, 4:49 PM
Luke Hegelund
Luke Hegelund - avatar
+ 2
@Andrew he means in C#. In JS it's string.split(' ').join('');
12th Apr 2017, 8:33 PM
Max_N
Max_N - avatar
+ 2
To solve this, we have to replace the space with nothing using string function str.Replace(" ","");
29th Apr 2017, 2:28 PM
Palla Surya Venkata Satyanarayana
Palla Surya Venkata Satyanarayana - avatar
0
" "
5th Apr 2017, 6:52 PM
lloiikki
lloiikki - avatar
0
u can also use strcpy() in cpp
12th Apr 2017, 10:39 AM
Ajmal Aboobacker
Ajmal   Aboobacker - avatar
0
hmm, try a no space method, or a different language
18th Apr 2017, 9:49 PM
Nicholas Begg
Nicholas Begg - avatar
0
use trim method
25th Apr 2017, 5:11 AM
Hammad
Hammad - avatar
0
@Hammad the String.Trim() method replaces the whitespace characters on the FRONT and on the END of the string, but NOT INSIDE it. Example: " Hello World! ".Trim() returns: "Hello World!"
25th Apr 2017, 2:37 PM
Magyar DĂĄvid
Magyar DĂĄvid - avatar
0
Why turkısh dont language
1st May 2017, 9:58 AM
Mustafa Özugur
Mustafa Özugur - avatar
- 1
I guess that Console.WriteLine (string.Empty) too
23rd Apr 2017, 4:58 PM
Lillium Sophia