C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, you can obtain the length of a string using the Length
property of the System.String
class. The Length
property returns the number of characters in the string. In this tutorial, we'll cover the basics of using the Length
property effectively.
The Length
property is a read-only property that represents the number of characters in the current string. The property's signature is:
public int Length { get; }
Example:
string text = "Hello, World!"; int length = text.Length; // 13
The Length
property is commonly used in loops for iterating over the characters in a string:
string text = "Hello, World!"; int length = text.Length; for (int i = 0; i < length; i++) { char currentChar = text[i]; Console.WriteLine($"Character at index {i}: {currentChar}"); }
The Length
property can also be used for conditional checks, such as validating user input or checking for empty strings:
string userInput = Console.ReadLine(); if (userInput.Length == 0) { Console.WriteLine("Please enter a value."); } else if (userInput.Length < 5) { Console.WriteLine("The input must be at least 5 characters long."); } else { Console.WriteLine($"You entered: {userInput}"); }
In this tutorial, we covered how to obtain the length of a string using the Length
property in C#. The Length
property is useful for iterating over characters in a string, validating user input, and performing conditional checks.
How to get string length in C#
The length of a string in C# represents the number of characters it contains.
string myString = "Hello, C#"; int length = myString.Length; // Returns 10
Using string.Length property in C#
The Length
property is used to retrieve the length of a string in C#.
string text = "C# Programming"; int length = text.Length; // Returns 15
C# string length vs. Count vs. Length()
In C#, string.Length
is used for strings, while Count
is commonly used for collections, and Length()
is used for arrays.
string myString = "C# is great"; int length = myString.Length; // Returns 12
Getting the length of a string array in C#
Obtain the length of a string array using the Length
property.
string[] stringArray = { "C#", "Java", "Python" }; int arrayLength = stringArray.Length; // Returns 3
Calculating total characters in a multiline string in C#
Use Length
to calculate the total characters in a multiline string.
string multilineText = "C#\nProgramming\nLanguage"; int totalCharacters = multilineText.Length; // Returns 25
String length and Unicode characters in C#
The Length
property considers Unicode characters, providing accurate lengths.
string unicodeString = "C# �ַ���"; int unicodeLength = unicodeString.Length; // Returns 8
Handling null or empty strings with string.Length in C#
Check for null or empty strings before using Length
to avoid exceptions.
string myString = null; int length = myString?.Length ?? 0; // Returns 0
C# string length and StringBuilder
StringBuilder
also has a Length
property for obtaining the length.
StringBuilder sb = new StringBuilder("C# StringBuilder"); int length = sb.Length; // Returns 16
Determining byte length of a string in C#
To get the byte length of a string, you can use the Encoding
class.
string myString = "C# �ַ���"; int byteLength = Encoding.UTF8.GetByteCount(myString); // Returns 17
Getting substring lengths with string.Length in C#
Length
can be used to get the length of substrings.
string mainString = "C# Programming"; string subString = mainString.Substring(3, 5); int subStringLength = subString.Length; // Returns 5
C# string length in Windows Forms and WPF applications
In GUI applications, string length can be obtained as usual.
string userInput = textBox.Text; int userInputLength = userInput.Length; // Get length from a TextBox
String length in LINQ queries in C#
Use Length
in LINQ queries to filter or manipulate strings based on length.
var longStrings = from str in stringList where str.Length > 5 select str;