C# > Strings

String in C#

How to use different string methods in C#?


String

String is an object of data type used in the programming such as integer and floating point unit which is used to represent the text rather than numbers, It is defined as set of characters for example "techfunda" or "1234" both can be strings. Strings must be closed in quotation marks to identgify it as a string.      

Example program to define a string:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class SimpleStringTest
    {
        static void Main()
        {
            string a = "Tech";
            string b = "funda";
            Console.WriteLine(a + b);
            Console.WriteLine(a + b == "Techfunda");
        }

    }

In the above code snippet we have defined strings. We have taken class as simplestringTest and in the main function we have taken string a ="Tech" and string b as "funda" and we are giving the console.writeline as (a+b). which adds the values of string a and string b, The console.Writeline (a+b=="Techfunda") which  is used to return true or false if the string a and string b value is equal as techfunda it prints as true and if the values of string are not equal it prints as false.  

output

String Comparision

String comparision is used to compare two string values.String comparision is often used to compare file names, path names, network paths.

Example program on string comparsion :  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
        string s1 = "Techfunda";
        string s2 = "Techfunda";
        Console.WriteLine(s1 == s2);
        
    }
}

In the above code snippet we have defined the string comaparision. We have taken class as program and in the main function  we have taken  as string s1 = "techfunda" and string s2 = "techfunda" and in the nextline we have given the console.writeline(s1==s2) which compares the string s1= string s2 if the string s1 and string s2 are equal it prints the result as true and if the string s1 and string s2 values are not equal then it prints the value as false.  

output

String concatenation

String concatenation is used to add two string values. Concatenation is used for the purpose of saving the space.

Example program on string concatenation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {

        string s1 = "2";
        string s2 = "TechFunda" + s1;
        String s3 = "1";
        string s4 = "DotnetFunda";
        Console.WriteLine(s4 + s3 + s2);
    }
}

In the above code snippet we have defined string concatenation. We have taken class as program and in the main function we have taken string s1 = 2 and string s2 = techfunda +s1 and string s3 = 1 and string s4 = dotnetfunda  and the nextline console.Writeline (s4+s3+s2) which adds the string values of (s4+s3+s2) in prints the result in the output.     

output

String.Substring

A substring is defined as a part of longer string. It is used  to extract the subset of the string. The initilization of substring is  substr().

Example program on substring:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class program
    {
        static void Main(string[] args)
        {
            string str = " MR.Sheo narayan is the founder of Techfunda and dotnetfunda ";
            Console.WriteLine(str);
            string substr = str.Substring(4);
            Console.WriteLine(substr);
        }
    }

In the above code snippet we have defined the substring. We have taken class as program and in the main function we have defined string value as "Mr.sheo narayan is the founder of Techfunda and dotnetfunda". The console.writeline(str)  prints the string value , The nextline of string substr=str.substring(4) removes the first 4 letters of the string value. The console.Writeline(substr) prints the string substr value as sheo narayan is the founder of Techfunda and dotnetfunda

output

Split string

split string is used to split the words from one to another. The initilization of the split string is string.split(). 

Example program on splitstring:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
        string s = "Sheo narayan is the founder of techfunda and dotnetfunda";
        string[] words = s.Split(' ');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

In the above code snippet we have defined the splitstring. We have taken class as program and in the main function we have taken string as "sheo narayan is the founder of the techfunda and dotnetfunda".   The string[]words = s. split(' '); prints the string as spliting into the nextline. foreach(string word in words) represents the word in words and the last step Console.Writeline (word) prints the output value as line by line.

output

String.Join

String join is used to join many methods in to one method.The initilization of string join is stringjoin

Example program on string join:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        string[] arr = { "dotnetfunda", "Techfuda", "Itfunda" };
        
        Console.WriteLine(string.Join(",", arr));

       
    }
}

In the above code snippet we have defined the string join method. We have defined class as program and in the main function we have taken string[] array as single string which are divided in to the characters and the the console.Writeline (string.join (",", arr) defines the output with separating commas.   

output

String Array

Array cotains strings which consists of int values, arrays are also used as strings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
    {
        static void Main()
        {
            string[] names = new string[5];

            names[0] = "dotnetfunda";
            names[1] = "techfunda";
            names[2] = "farmingfunda";
            names[3] = "kidsfunda";
            names[4] = "itfunda";

            Console.WriteLine(names[2]);
            Console.WriteLine(names[4]);
        }
    }

In the above code snippet we have defined string array. We have taken class as program and in the main function we have defined string[] names=new string [5]   which defines the new string values as names[0]=dotnetfunda, names[1]=techfundanames[2]=farmingfundanames[3]=kidsfundanames[4]=itfunda. In the nextline we are the console.writeline (names[2]) calls the value of farmingfunda and the console.writeline (names[4]) calls the itfunda   

output

StringBuilder

String builder is used to modify the string with out creating any object. String builder can boost the performance when concatinating many strings together in a loop, A string builder is used to build the long string using Append. The initilization of string builder is string.builder().

Example program on string builder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 20; i++) sb.Append(i + ",");
        
        Console.WriteLine(sb);
    }
}

In the above code snippet we have defined the string bulider. We have taken class as program and in the main function w have taken string builder sb = new string builder();   We are assigning the string builder as sb to new string builder() and we are checking the for (int i=0; i<20; i++) to check the i values which are equal to 20  and if the i value is not equal to 20 it increments wit the condition i++ and sb.Append(i + ","); Append is used to add the new line sequence  and console.writeline(sb) prints the value of sb in the output.     

output


String.Format

String format is used to convert the value of an object, variable, expression in to the string by using different formats.

The initilization of stringformat is string.Format().

Example program on string.Format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
        int i = 25;
        string value = string.Format("string = {0:0.2%}",i);
        Console.WriteLine(value);
    }
}

In the above code snippet we have defined the string.format. In this program we have taken class as program and in the main function we have taken int i value as 25 and in the nextline we have declared the string value=string.Format("string={0:0.2%}",i); which defines as string.Format is used to convert the value of an object to another string as ("string {0:0.2%}" ,i) converts the string value as 0 is 25 and 0.2 as 252 and % denotes as multiply the value with 100 ,i value and the consol.writeline (value) prints the value in the output.

output

 


String.Replace

String Replace is used to replace the values. If we have given string as "name" and if we want to replace it we use string .replace as another "name".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
        string R = "Swatch bharat";
        Console.WriteLine(R);

        string v = R.Replace("Swatch", "clean");
        Console.WriteLine(v);
    }
}

In the above code snippet we have defined String.Replace. We have taken class as program and in the main function we have givenstring R = "swatch bharat". and we have given console.writeline as (R) which prints the output as Swatch bharat  and in the next line w have given string v as R.Replace("swatch", "clean"). which replaces the name string swatch with clean and prints the output by giving the console.writeline as clean bharat.  

output:

String.Copy

String copy is used to copy the value of one string to another string. The initilization of strinng copy is String.Copy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
       
        string message1 = "techfunda";
        string message2 = "dotnetfunda";
        string message3 = string.Copy(message1);

        Console.WriteLine(message1);
        Console.WriteLine(message2);
        Console.WriteLine(message3);
    }
}

In the above code snippet we have defined the string copy method. We have taken class as program and in the main function we have taken string message1 as techfunda and string message 2 as dotnetfunda and string message3 as stringcopy of message1 which copies the value of the message1 and the console.writeline prints the value of message 1 and message 2 and the consol.writeline of message 3 prints the value of message 3 as techfunda   

 

output

String.Remove

String remove is used to remove the string from given string. The inilization of the stringremove is stringRemove.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    static void Main()
    {
       
        string string1  = "techfunda";
        string result = string1.Remove(string1.Length -1 );

        Console.WriteLine(result);

    }
}

In the above code snippet we have define the StringRemove method. we have taken class as program and in the main function we have defined the string as stringh1 as techfunda and stringresult as string .Remove (string1.Length-1) which removes the letter from the given string. and the console.writeline prints the output of result as techfund   

output

 Views: 5672 | Post Order: 14



Write for us






Hosting Recommendations