SEARCH C# PROGRAMMING CODES HERE

Strings

String in C#

The string is a keyword which is used to address a sequential amassing of characters that are known as a substance, and the string is an object of the System. String type. The string factors in c# can hold any sort of content and by utilizing Length property we can realize that the number of characters of the string variable is holding dependent on our prerequisites. We can perform numerous tasks on strings, for example, connection, comparison, getting substring, seek, trim, substitution and so forth.

1-Initializing a String object using Keywords.

Declare a string keyword name first and then initialize a string by calling string keyword name, using functions.so let's see an example which shows to initialize a String as follows.


Program

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)

        {
            string str1 = "Hello";

            Console.WriteLine(str1);
          
            Console.ReadKey();

        }
           
       }
    }

Output

Hello

2-Initializing a String object using String Class

Now we will see an example to initialize a string using String Class as follows.

Program

using System;  
public class String  

{  

    public static void Main(string[] args)  

    {  

            char[] chr = { 'H', 'e', 'l', 'l', 'o' };

            string str1 = new string(chr);

            Console.WriteLine(str1);

            Console.ReadKey();

      }  

}  

Output

Hello

String Objects Conversion

There are numerous methods available in C#  that will help you in working with the string objects.

Here are some of the useful predefined functions are shown below which you can implement in your program.

Methods

1-ToUpper();

This function is used to convert the string into uppercase character. For Example, you can see the program.

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)
        {
            string str1= "hello";
            string str5 = str1.ToUpper();
            Console.WriteLine(str5);  
            Console.ReadKey();
        }     
       }
 }


Output

HELLO



2-ToLower();

In the above program, we have converted the string characters into uppercase, Similarly to convert from lowercase to uppercase you can implement (ToLower())function, For Example, you can see the program.

using System;

namespace program

{
    class Program

    {
        static void Main(string[] args)

        {
            string str1 = "HELLO";
            string str5 = str1.ToLower();
            Console.WriteLine(str5);  
            Console.ReadKey();
        }     
    }
 }

Output

hello



3-ToString();

This function is used to to get the instance of the String. For Example, you can see the program.

using System;

namespace program
{
   class Program
    {
        static void Main(string[] args)
        {
           string str1 = "Hello";
            int x = 100;
            string str2 = str1.ToString();
            string str3 = x.ToString();
            Console.WriteLine(str2);
            Console.WriteLine(str3);      
            Console.ReadKey();
       }
    }
 }

Output

Hello

100



4-Replace();

This function is used to replace string object with the another specified string object. For Example, you can see the program.

using System;

namespace program

{   class Program
    {
        static void Main(string[] args)
        {
            string str1 = "Hello ";
            string str2 = str1.Replace("Hello", "Wellcome");
            Console.WriteLine(str2); 
           Console.ReadKey();
             
        }
           
    }
 }

Output

Wellcome



5-Split();

This function is used to split a string into substrings on the basis of characters in an array. It returns a string array. The split method can be implemented by the declaring element inside the separator using a single code(' ')to split from. In the below program I have declared comma sign to split from (','). For Example, you can see the program.

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = ("One,Two,Three,Four,Five");
            string[] str2 = str1.Split(',');       /*Declared comma sign to split from (',')*/

            foreach (string str3 in str2)
            {
                Console.WriteLine(str3);
            }  
               Console.ReadKey();        
        }      

    }

 }



Output

One

Two

Three

Four

Five

6-SubString(); 

It is used to get a substring from a String and also it starts from the specified character position to the end of the string. In the below program I have declared 7 characters as ABCDEFG without giving any spaces in between to show you how it works and in the second line of code I have specified character position as(5) substring character its means it will start counting after the 5th position of character and will keep counting till the end and also keep in mind that its count space expressions as well. For Example, you can see the program below.

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)
        {
           string str1 = "ABCDEFG";
            string str2 = str1.Substring(5);
            Console.WriteLine(str2);  
            Console.ReadKey();
       }         
    }
 }


Output

FG

7-JoinString();

It utilized to connect the elements of an array, using the specified separator between each element. For Example, you can see the program.

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s1 = { "A", "B", "C" };
            string s3 = string.Join("=", s1);
            Console.WriteLine(s3);  
            Console.ReadKey();
        }   
    }
 }

Output

A=B=C



8-Copy();

It is to used to make another event of String with a similar incentive as a predetermined String. It is a static system for String class. Its entry type is a string. For Example, you can see the program as follows.

Program

using System;

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "Wellcome";
            string str2 = string.Copy(s1);
            Console.WriteLine(str1+"\n"+str1);
            Console.ReadKey();
        }
          
    }
 }

Output
Wellcome
Wellcome


9-Remove();

It is used to utilize to get another string in the wake of expelling every one of the characters from the determined starting Index until given length. In the event that length isn't determined, it expels every one of the characters after starting Index. For Example, you can see the program as follows.


Program

using System;

namespace program

{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "Wellcome";
            string str2 = str1.Remove(4);
            Console.WriteLine(str2);  
            Console.ReadKey();
        }
          
    }
 }

Output

Well



10-Insert();

It is used to utilized to embed the predefined string at an indicated record number. The record number begins from 0. Subsequent to embeddings the predefined string, it restores another changed string. For Example, you can see the program as follows.


Program

using System;

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "";  
          string str2 = str1.Insert(0,"Wellcome");  
          Console.WriteLine(str2);  
            Console.ReadKey();
        }
           
    }
 }



Output

Wellcome