vb.net 删除   来自 VB 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28478814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:48:02  来源:igfitidea点击:

remove   from String in VB

vb.netstringreplacewhitespace

提问by Pramod

I have inserted a option in Dorpdown as follows

我在 Dorpdown 中插入了一个选项,如下所示

<option>????&nbsp;&nbsp;&nbsp;&nbsp;</option>

When I select this text from server side on any event I get this value "????????????"

当我在任何事件中从服务器端选择此文本时,我都会得到此值“????????????”

Now I want to replace this white space in the string. I have tried replace method of String class. But its not working.

现在我想替换字符串中的这个空格。我尝试过 String 类的替换方法。但它不工作。

str = str.replace("&nbsp;","")

Plz suggest

请建议

回答by Willem Van Onsem

What you should do first is decode the HTML, such that text like &nbsp;but also &amp;are converted to their textual counterparts (" "and "&"). You can do this with: WebUtility.HtmlDecode. Next you can use String.Trimto remove leading and tailing spaces.

您首先应该做的是解码 HTML,这样文本 like&nbsp;但也&amp;被转换为它们的文本对应物(" ""&")。你可以这样做:WebUtility.HtmlDecode。接下来您可以使用String.Trim删除前导和尾随空格。

Example:

例子:

string s = "????&nbsp;&nbsp;&nbsp;&nbsp;";
string r = WebUtility.HtmlDecode(s).Trim();

Or the VB.NET equivalent:

或 VB.NET 等价物:

Dim s As String = "????&nbsp;&nbsp;&nbsp;&nbsp;"
Dim r As String = WebUtility.HtmlDecode(s).Trim()

Evidently you can try to convert &nbsp;to spaces yourself. But there are examples where it is not that evident and your transcoder can get confused or decode strings the wrong way. Furthermore if in the future the people at W3C change their minds about how to encode text in HTML/XML, then your program will still work.

显然,您可以尝试自己转换&nbsp;为空格。但是有些例子并不那么明显,您的转码器可能会混淆或以错误的方式解码字符串。此外,如果将来 W3C 的人们改变了关于如何在 HTML/XML 中编码文本的想法,那么您的程序仍然可以运行。

String.Trimwill remove all kinds of white-spaceincluding spaces, new lines, tabs, carriage returns, etc. If you only want to remove spaces, you can use: .Trim(' '). Then you specify only to remove the given list of characters (here only ' ').

String.Trim将消除各种空白,包括空格,换行,跳格,回车等,如果你只是想去掉空格,你可以使用:.Trim(' ')。然后您指定仅删除给定的字符列表(此处仅' ')。

回答by Tim Schmelter

If you want to remove leading or trailing white-spaces from a string you just need to use String.Trim, but you have to re-assign the return value to the variable since strings are immutable:

如果您想从字符串中删除前导或尾随空格,您只需要使用String.Trim,但您必须将返回值重新分配给变量,因为字符串是不可变的:

string text = "????????????";
text = text.Trim();

Note that you can also use TrimEndin this case.

请注意,您也可以TrimEnd在这种情况下使用。

If you want to remove only space characters(not also tabs or new-line characters which are also white-spaces) use:

如果您只想删除空格字符(不是制表符或也是空格的换行符),请使用:

text = text.Trim(' ');

If you instead want to remove all spaces from a string you could do:

如果您想从字符串中删除所有空格,您可以执行以下操作:

text = text.Replace(" ", "");

回答by J3soon

I think maybe your code is something like this

我想也许你的代码是这样的

Dim str As String = "????&nbsp;&nbsp;&nbsp;&nbsp;"
str.Replace("&nbsp;", "")

But actually you should

但实际上你应该

Dim str As String = "????&nbsp;&nbsp;&nbsp;&nbsp;"
str = str.Replace("&nbsp;", "")

回答by Vityata

I have just had a similar problem. It turns out, that this nbsp character is Chr(160) from the ASCII table. Thus, something like this is quite meaningful, for all the cases. It works, on a selected area:

我刚刚遇到了类似的问题。事实证明,这个 nbsp 字符是来自 ASCII 表的 Chr(160)。因此,对于所有情况,这样的事情都非常有意义。它适用于选定区域:

Public Sub remove_space_in_string()

    Dim r_range As Range

    For Each r_range In Selection
        r_range = Trim(r_range)
        r_range = Replace(r_range, vbTab, "")
        r_range = Replace(r_range, " ", "")
        r_range = Replace(r_range, Chr(160), "")
    Next r_range
End Sub