You must Sign In to post a response.
  • Category: .NET

    About C# regarding string

    How can i concatenate two string without using predefined function using c#
  • #766060
    Please any one can send me the logic

  • #766065
    Hi,

    If you want to concatenate 2 strings then use below sample


    "Welcome," + UserName + " to the Microsoft world."

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766066
    There are multiple ways to get it
    1. using + literal
    see below snippet

    using System;
    class Program
    {
    static void Main()
    {
    // ... Create a new string reference.
    // It points to the literal.
    string s1 = "string2";

    // ... Add another string to the start.
    string s2 = "string1" + s1;
    Console.WriteLine(s2);
    }
    }

    2. Or simply you can create a loop on a string to make it concatenate with other.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766074
    You can use the "+" for concatenating two simple strings.
    Following is the sample concatenation of two strings.

    using System;
    class Program
    {
    static void Main()
    {
    string MyString1 = "String 1 ";
    string MyString2 = "String 2 ";
    Console.WriteLine(MyString1 + MyString2 );
    }
    }

    If you want to concatenate many strings into one string, do not go for string concatenation you may face performance issue. For that you can go for StringBuilder.

    StringBuilder MyStringBuilder = new StringBuilder("My String 1");
    MyStringBuilder.Append(" My String 2");
    MyStringBuilder.Append(" My String 3");
    Console.WriteLine(MyStringBuilder);

    By Nathan
    Direction is important than speed

  • #766078
    Hi,

    1)Using "+"

    static void Main(string[] args)
    {
    // To run this program, provide a command line string.
    // In Visual Studio, see Project > Properties > Debug.
    string userName = args[0];
    string date = DateTime.Today.ToShortDateString();

    // Use the + and += operators for one-time concatenations.
    string str = "Hello " + userName + ". Today is " + date + ".";
    System.Console.WriteLine(str);

    str += " How are you today?";
    System.Console.WriteLine(str);

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
    }

    // Example output:
    // Hello Alexander. Today is 1/22/2008.
    // Hello Alexander. Today is 1/22/2008. How are you today?

    2) Using string Builder
    class StringBuilderTest
    {
    static void Main()
    {
    string text = null;

    // Use StringBuilder for concatenation in tight loops.
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    for (int i = 0; i < 100; i++)
    {
    sb.AppendLine(i.ToString());
    }
    System.Console.WriteLine(sb.ToString());

    // Keep the console window open in debug mode.
    System.Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
    }
    }

  • #766104
    Hi

    Try this piece of code

    in C#



    string SS1 = "ASP.NET";
    string SS2 = "C#/VB";

    string ss3 = SS1 + SS2;



    in Sql Means try this code



    SELECT BookName + ' ' + Category FROM BOOKS

    SELECT (BookName + ' ' + Category) FROM BOOKS

    SELECT BookName+Category FROM BOOKS


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766183
    Hi
    You can use + operator to concat string

    e.g string s1="Donet";
    string s2="Spider";
    Console.Writeline(s1+s2);
    o/p-DonetSpider

    there is performance issue for string class if data is more. so in that case you can go for StringBuilder class
    which having Append() method

    hope it will help you

    Thanks
    Umesh Bhosale

  • #766247
    Hello,

    You can try this :- (just mentioning that part of the code)

    string str = "Hello";
    string str1 = "India";
    string resultantStr = str + " " + str1;
    Console.Writeline(resultantStr);

    Thanks

  • #766552
    HI,

    In .net it's very easy to concant tow string with out any built in functions. Becoz, we have a Concat Opertor as "+" default. Kindly use that one for Concat a two Strings.

    Example:
    String s1="Dotnet";
    String s2="Develpoer";
    String s3="concat String Is :"+s1+s2
    Console.WriteLine(s3);

    Regards,
    Karunanidhi.K


  • Sign In to post your comments