C# - 在两个字符串之间添加一个空格

时间:2018-04-19 14:51:13

标签: c# uwp

我有通过Make和Model显示车辆的代码。

    <body>
    <div class="container">
    <br><br>
    <div class="row">
      <div class="col-md-12">
        <div class="panel panel-default">
          <div class="panel-body">
            <form class="form-horizontal">
              <div class="col-md-12">
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
                  <div class="col-sm-10">
                    <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
                  <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                      <label>
                      <input type="checkbox"> Remember me         </label>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">Sign in</button>
                  </div>
                </div>
              </div>
            </form>

          </div>
        </div>
      </div>
      <div class="col-md-12">
        <div class="panel panel-default">
          <div class="panel-body">
            some other content here some other content here some other content here some other content here some other content here some other content here some other content here some other content here some other content here some other content here some other
            content here some other content here
          </div>
        </div>
      </div>
      <!-- 2nd -->

      <div class="col-md-9">
        <div class="panel panel-default">
          <div class="panel-body">

            <form class="form-horizontal">
              <div class="col-md-12">
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-3 control-label">Email</label>
                  <div class="col-sm-9">
                    <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-3 control-label">Password</label>
                  <div class="col-sm-9">
                    <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-3 col-sm-9">
                    <div class="checkbox">
                      <label>
                      <input type="checkbox"> Remember me</label>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-3 col-sm-9">
                    <button type="submit" class="btn btn-default">Sign in</button>
                  </div>
                </div>
              </div>
            </form>

          </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="panel panel-default">
          <div class="panel-body">
            <img src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" width="200" height="200" class="img-responsive" alt="Generic placeholder thumbnail">
          </div>
        </div>
      </div>
    </div>
  </div>


</body>

以上代码显示的文字如下:&#34; BentleyContinental&#34;,如何将文字显示为&#34; Bentley Continental&#34;。

3 个答案:

答案 0 :(得分:10)

您可以使用string.Format()

productName.Text = string.Format("{0} {1}", p.Make, p.Model);

或者您可以使用string interpolation(如果您使用的是C#6或更高版本):

productName.Text = $"{p.Make} {p.Model}";

或者你可以像你一样(字符串连接)但添加空格:

productName.Text = p.Make + " " + p.Model;

答案 1 :(得分:3)

使用ShellViewModel方法连接字符串部分。

string.concat

一般情况下,当您知道添加少于十几个部分时,可以使用productName.Text = string.concat(p.Make, " ", p.Model); 。不仅如此,或者如果您处于循环中,使用string.concat类还有更多好处。

答案 2 :(得分:0)

productName.Text = p.Make  + " " + p.Model

只需将两个单词之间的空格连接起来。我认为这是最简单的方法。