创建一个字符串并将文本附加到它 [英] Create a string and append text to it

查看:19
本文介绍了创建一个字符串并将文本附加到它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有趣的是,我有一个文本框,我可以在其中附加字符串.

Funny, I had a textbox and I could append strings to it.

但现在我创建了一个这样的字符串:

But now I create a string like this:

    Dim str As String = New String("")

我想向它附加其他字符串.但是没有这样做的功能.我做错了什么?

And I want to append to it other strings. But there is no function for doing so. What am I doing wrong?

推荐答案

Concatenate with &操作员

Dim str as String  'no need to create a string instance
str = "Hello " & "World"

您也可以使用 + 运算符进行连接,但在尝试连接数字时可能会遇到麻烦.


连接 String.Concat()

str = String.Concat("Hello ", "World")

连接字符串数组时很有用


StringBuilder.Append()

连接大量字符串时使用 StringBuilder,它会带来更好的性能.

When concatenating large amounts of strings use StringBuilder, it will result in much better performance.

    Dim sb as new System.Text.StringBuilder()
    str = sb.Append("Hello").Append(" ").Append("World").ToString()

.NET 中的字符串是不可变的,导致每次连接都会实例化一个新的 String 对象,并对其进行垃圾回收.

Strings in .NET are immutable, resulting in a new String object being instantiated for every concatenation as well a garbage collection thereof.

这篇关于创建一个字符串并将文本附加到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆