Click here to Skip to main content
14,739,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone tell me how can I know the numbers of characters in a string entered by the user in C# code?Like if i take a string from the user and he writes "CODE" so it has 4 characters. So how can i know ?

What I have tried:

I just google because I don't know anything about it as I was dealing strings with arrays and there only codes are available for strings not taken by the user.
Posted
Updated 19-Apr-20 6:03am

A string is a string, is a string: it doesn't matter where is came from - the user via a Console.ReadLine, a textbox.Text property, a Combobox.Value, or a static constant you typed before your app compiled.
In you app it's an instance of a string, that's all - so all the string properties and methods are available to you: String Class (System) | Microsoft Docs[^]
That includes the string.Length property:
string read = Console.ReadLine();
string typed = MyTextBox.Text;
string compiled = "CODE";
Console.WriteLine($"{read.Length}:{typed.Length}:{compiled.Length}");
   
Comments
Member 14806683 20-Apr-20 3:34am
   
Ooh Thanks! a lot.I am so grateful to you.But My text book is giving error so I just neglected that and found my answer.Again thanks
OriginalGriff 20-Apr-20 3:48am
   
You're welcome!
Use the Length property of the string class:
String.Length Property (System) | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900