Home > Backend Development > C#.Net Tutorial > C# string properties

C# string properties

PHPz
Release: 2023-09-15 19:25:02
forward
888 people have browsed it

C# 字符串属性

The following are the properties of the String class in C#-

##12
Sr.No Properties&Description

CharsGet the Char object at the specified position in the current String object.

LengthGet the number of characters in the current String object.

Example

Let’s see an example-

Live Demonstration

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");
      }
   }
}
Copy after login

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
Copy after login

Example

Now let’s see another example-

Live Demonstration

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);
   }
}
Copy after login

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
Copy after login

The above is the detailed content of C# string properties. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template