STRINGS (CONCATENATION) IN C#

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted by +Manish Kumar Gautam +LIV WIRE +Asp.net Solutions


Click imaginationhunt.blogspot to see latest Blogs  

CONCATENATE STRINGS

Strings concatenation means joining and adding two different strings to make a single string.



Practical Implementation:

using System;

namespace AspnetSolutions
{
    class ConcatenateString
    {
        public static void Main()
        {
            string str1 = "imaginationhunt.";
            string str2 = "blogspot.in";

            Console.WriteLine(str1+str2);

        }
    }
}


Output

Use Concat() Method:
This method is used to concatenate string. Concat() method reside in String class. It has various overloaded constructor. It accepts object parameter and returns a single string

Click imaginationhunt.blogspot to see latest Blogs  

Practical Implementation: 

using System;

namespace AspnetSolutions
{
    class ConcatenateString
    {
        public static void Main()
        {
            string str1 = "imaginationhunt.";
            string str2 = "blogspot.in";

            int length = 25521;
 
            Console.WriteLine(str1+str2);

            Console.WriteLine(String.Concat(str1,str2));
            Console.WriteLine(String.Concat(str1,str2,length));


        }
    }
}


Output



To read STRINGS (CONCATENATION) IN C# Part 2 - Click here.

Related Questions:


 

Q-1 Select the output for the given set of code:

static void Main()
{
string s1 = "Hello"+"Asp.net"+"Solutions";
System.Console.WriteLine(s1);
System.Console.ReadLine();

}

A) HelloAsp.netSolutions
B) Hello Asp.net Solutions
C) Compile time error
D) Hello
Ans- Option(A).


Q-2 Which string operation does the below mentioned method defines? More than one option

public static string Concat(string str0, string str1)
 
a) method returns a string
b) string str1 is concatenated to the end of str0
c) can be used to concatenate any number of strings
d) None of the mentioned


Click imaginationhunt.blogspot to see latest Blogs

Ans- Option (A), (B), (C).
Explanation: (A) and (B) are correct because this method returns a string that contains str1 concatenated to the end of str0. 
Now, look at another form of Concat(), which concatenates three strings:
public static string Concat(string str0, string str1, string str2). 
Hence, any number of strings can be concatenated using this method. So option(C) is also correct.

 Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions