Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# String Properties
Following are the properties of the String class in C# −
| Sr.No | Property & Description |
|---|---|
| 1 |
Chars Gets the Char object at a specified position in the current String object. |
| 2 |
Length Gets the number of characters in the current String object. |
Example
Let us see an example -
using System;
public class Demo {
public static void Main() {
string str1 = "h8b9";
string str2 = "abcdef";
Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
bool val = str1 != str2;
Console.WriteLine("Is str1 equal to str2? = "+val);
for (int i = 0; i < str1.Length; i++) {
if (Char.IsLetter(str1[i]))
Console.WriteLine(""+str1[i]+" is a letter");
else
Console.WriteLine(""+str1[i]+" is a number");
}
}
}
Output
This will produce the following output -
Is string1 null or empty? = False Is string2 null or empty? = False Is str1 equal to str2? = True h is a letter 8 is a number b is a letter 9 is a number
Example
Let us now see another example -
using System;
public class Demo {
public static void Main() {
string str1 = "hijklm";
string str2 = String.Empty;
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
Console.WriteLine("String1 length = "+str1.Length);
Console.WriteLine("String2 length = "+str1.Length);
Console.WriteLine("String length = "+"demo".Length);
}
}
Output
This will produce the following example -
Is string1 null or whitespace? = False Is string2 null or whitespace? = True String1 length = 6 String2 length = 6 String length = 4
Advertisements