Concept of Substring and Concat methods using examples

 

Introduction

In this article we will try to understand string class methods, that is concat and substring methods in c-sharp using a source code example in c-sharp.

Substring method and Concat method are the methods of string class. So we need to use "string" data-type to call both methods.

Concat Method

  • It is method of a string class in c#
  • It concatenates two or more strings as single unit of string.
  • It can also concatenate string arrays.
  • We have to call "concat" method using dot operator to string data type.

Syntax of concat method

 
string output = string.Concat(string1,string2,string3…);
string[] s = new string[4] { "Welcome  ", "to ", "Questpond ", "Session " };
String output = string.Concat(s);

Example of Concat Method using C# code

We will be using Console Application for this example.

First step is to create two separate strings as shown in below snippet of code.

 
static void Main(string[] args)
{
  string str1 = "Questpond";
  string str2 = "Online Sessions";
}

Next step is to create output string which can handle output of "string.Concat" method as shown below snippet.

 
static void Main(string[] args)
{
  string str1 = "Questpond ";
  string str2 = "Online Sessions";
  string output = string.Concat("Output as ",str1, str2);
}

As you can see we have combined two separate string "str1, str2" and custom string "output as" into a single unit as new string output using "string.Concat" method.

Display Output

Substring Method in C#

  • It is method of a string class in c#
  • Substring extracts existing strings
  • It requires start index and a length of string then it will return completely new string with the characters in that range.
  • It is called by string name followed by dot operator.

Syntax of substring method

 
static void Main(string[] args)
{
  string input = "QuestpondOnlineSession";
  string sub1 = input.Substring(start index,length of string);
}

Example of Substring Method using C# code

We will be using Console Application for this example.

First step is to create string which we want to extract it as shown in below snippet of code.

 
static void Main(string[] args)
{
  string input = "QuestpondOnlineSession";
}

Next step we will extract string in three ways using Substring method as shown below

 
static void Main(string[] args)
{
  string input = "QuestpondOnlineSession";
  string sub1 = input.Substring(0,9);
  string sub2 = input.Substring(9, 6);
  string sub3 = input.Substring(15);
}

String "sub1" which extracts string from index 0 of length 9.

String "sub2" which extracts string from index 9 of length 6.

String "sub3" which extracts string from index 15 and length here is not been specified.

Display Output

Hey friends..!This is all about substring and concat methods of a string class in c-sharp language. If you have any doubt feel free to ask me. If you like this article please share this article with your friends on FB, GPlus and twitter.

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Mar
10
2014

C# if and nested if conditional statements with an example?

CLICK TO READ FULL ARTICLE

Views - 11772 |Category - C#

Mar
08
2014

Practical - Count repeated words in a string using Regular Expression ?

CLICK TO READ FULL ARTICLE

Views - 6133 |Category - C#

Dec
07
2013

What are generic collections in C# using an example?

CLICK TO READ FULL ARTICLE

Views - 34705 |Category - C#

Dec
02
2013

What are generics in C# using an example?

CLICK TO READ FULL ARTICLE

Views - 35810 |Category - C#

Aug
25
2013

Use of string.Format method in C# with an example?

CLICK TO READ FULL ARTICLE

Views - 26452 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

mhkcv

 

 
Comments

Posted by Allwyn on 2014-03-19

Nice article bro