locked
Concatenate RRS feed

  • Question

  • User-797751191 posted

    Hi

       I have below code and want to conctenate Addresses in 1 like txtaddress = txtaddress1 + txtaddress2

    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
    sdr.Read();
    txtAddress.Text = sdr["Address"].ToString();
    txtAddress1.Text = sdr["Address1"].ToString();

    }

    Thanks

    Monday, June 24, 2019 4:21 AM

Answers

  • User-719153870 posted

    Hi jsshivalik,

    As far as I know, there are three common ways of splicing strings.

    1. Simple'+='splicing method

    txtaddress.Text = txtaddress1.Text.ToString() + txtaddress2.Text.ToString();

    In the case of simple string stitching, this method is the Easiest to implement.

    2.String.Format()

    txtaddress.Text = string.Format("{0}{1}", txtaddress1.Text.ToString(), txtaddress2.Text.ToString());

    The code in this way looks neat, easy to read, and much more efficient than ‘+=’.

    3.StringBuilder.Append

    using System.Text;

    StringBuilder str = new StringBuilder(); str.Append(txtaddress1.Text.ToString()); str.Append(txtaddress2.Text.ToString()); txtaddress.Text = str.ToString();

    In your case, the above three methods can effectively achieve string stitching, you can choose any one.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 5:40 AM

All replies

  • User-1038772411 posted

    Hi, jssshivalik

     var address = item.Address1 != null ? item.Address1 + "," : "";
                            var cityname = item.CityName != null ? item.CityName + "," : "";
                            var provincename = item.ProvinceName != null ? item.ProvinceName + "," : "";
                            var countryname = item.CountryName != null ? item.CountryName + "," : "";
                            var postalcode = item.Postal_code != null ? item.Postal_code + "," : "";
    
                            var mainaddress = address + cityname + provincename + countryname + postalcode;
                            var MainAdress = mainaddress != string.Empty ? mainaddress.Remove(mainaddress.Length - 1) : "";
    txtaddress.text = MainAdress;

    you can use sdr["Address"].ToString(); instead of item.Address1 and all related values that contains database value.

    Thanks.

    Monday, June 24, 2019 5:30 AM
  • User-719153870 posted

    Hi jsshivalik,

    As far as I know, there are three common ways of splicing strings.

    1. Simple'+='splicing method

    txtaddress.Text = txtaddress1.Text.ToString() + txtaddress2.Text.ToString();

    In the case of simple string stitching, this method is the Easiest to implement.

    2.String.Format()

    txtaddress.Text = string.Format("{0}{1}", txtaddress1.Text.ToString(), txtaddress2.Text.ToString());

    The code in this way looks neat, easy to read, and much more efficient than ‘+=’.

    3.StringBuilder.Append

    using System.Text;

    StringBuilder str = new StringBuilder(); str.Append(txtaddress1.Text.ToString()); str.Append(txtaddress2.Text.ToString()); txtaddress.Text = str.ToString();

    In your case, the above three methods can effectively achieve string stitching, you can choose any one.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 5:40 AM