Skip to main content

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

Popular posts from this blog

Media Player in C# Visual Studio By Rohit Programming Zone

Media Player  in C# Visual Studio   Source Code ----------------------------------------------------------------------------------------------------------------------------- using System; using System.Windows.Forms;   namespace Media_Player_in_C_Sharp_By_Rohit_Programming_Zone {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         string [] paths, files;           private void btn_open_Click( object sender, EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog ();   ...

ImageEditor in C# Visual Studio 2017

ImageEditor in C # Start a new project in Visual Studio > choose Visual C# then click on Windows Desktop then select WindowsForm App(.Net Framework)  and name it as ImageEditor then press ok. First, go to the Form1 properties  S ection and set the Size as following. Form1-size=952, 607 Now Add 3 panels from the toolbox and set the properties as follows. After setting the properties it will look as follows. Now Add 2-picture box from the toolbox and go to the properties and set the properties as follows. Now add 15-Button from the toolbox. Now add 3-textbox from the toolbox. Now add 3-trackbar and 5-label from the toolbox. Now go to the properties section and check all the Components are named correctly or not. Once  all the Components are named correctly then  move to the coding part. (Now the Coding Part) Now go to the Form1 code view and  ...

Microsoft tools Download

1-Visual Studio Code Visual Studio Code is a  fast and free  source code lite editor that can be used with the various programming languages. Download 2-Visual Studio 2019 Community Download 3-Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 Development environment specialized in Windows systems. Download 4-VISUAL STUDIO 2013 EXPRESS FOR WEB Visual Studio Express 2013 for Web provides the core tools for creating compelling, innovative web applications and services. This includes the ASP.NET editors for HTML5, CSS3, JavaScript, etc. Download 5-Piskel Download 6-Pichon Download 7-Microsoft XNA Framework Redistributable 4.0 Description The XNA Framework has all the necessary runtime components to execute a game, which was developed using Microsoft XNA Game Studio 4.0. Version 4.0 of the framework contains improved functionality and new features.