- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# String Concat with examples
To concat strings in C#, use the String.Concat() method.
Syntax
public static string Concat (string str1, string str2);
Above, the parameters str1 and str2 are the strings to be concatenated.
Example
using System; public class Demo { public static void Main(String[] args) { string str1 = "Jack"; string str2 = "Sparrow"; Console.WriteLine("String 1 = "+str1); Console.WriteLine("HashCode of String 1 = "+str1.GetHashCode()); Console.WriteLine("Does String1 begins with E? = "+str1.StartsWith("E")); Console.WriteLine("
String 2 = "+str2); Console.WriteLine("HashCode of String 2 = "+str2.GetHashCode()); Console.WriteLine("Does String2 begins with E? = "+str2.StartsWith("E")); Console.WriteLine("
String 1 is equal to String 2? = {0}", str1.Equals(str2)); Console.WriteLine("Concatenation Result = "+String.Concat(str1,str2)); } }
Output
String 1 = Jack HashCode of String 1 = 1167841345 Does String1 begins with E? = False String 2 = Sparrow HashCode of String 2 = -359606417 Does String2 begins with E? = False String 1 is equal to String 2? = False Concatenation Result = JackSparrow
Let us now see another example wherein we will use the concat() method with only a single parameter.
Syntax
public static string Concat (params string[] arr);
Above, arr is the string array.
Example
using System; public class Demo { public static void Main(string[] args) { string[] strArr = {"This", "is", "it", "!" }; Console.WriteLine("String Array..."); foreach(string s in strArr) { Console.WriteLine(s); } Console.WriteLine("Concatenation = {0}",string.Concat(strArr)); string str = string.Join("/", strArr); Console.WriteLine("Result (after joining) = " + str); } }
Output
String Array... This is it ! Concatenation = Thisisit! Result (after joining) = This/is/it/!
Advertisements