vb.net 如何判断回车是否在我的字符串中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29473909/
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
How to tell if carriage return is in my string?
提问by Criel
I have a textbox control that allows for the user of enter button to enter details like ADdresses or other demographic information. Since the default for addresses are as follows:
我有一个文本框控件,允许用户输入按钮输入详细信息,如地址或其他人口统计信息。由于地址的默认值如下:
Address 1
Address 2
City, st
Zip
I am wondering if there is a way to tell if the Enter key was used to make a new line here? I've looked around and currently the only is to have a check in VB for vbCrLf however I'm not seeing it pick this up in the code.
我想知道是否有办法判断 Enter 键是否用于在此处创建新行?我环顾四周,目前唯一的方法是在 VB 中检查 vbCrLf,但是我没有看到它在代码中找到它。
Test data for this would be something similar to below
对此的测试数据将类似于以下内容
123 N Street
S Test Street
Test City, XX
91883
The code below is what I'm trying to just replace any return carriage and replace with a space
下面的代码是我试图替换任何回车并替换为空格的代码
Text.Replace(vbCrLf, " ")
Will this vbCrLf not pick up a carriage return unless there's an actual space between the above test values?
除非上述测试值之间存在实际空格,否则此 vbCrLf 是否不会回车?
回答by Steve
If you are using a MultiLinetextbox (as it seems from your sample) then you don't need to search for the newline characters and replace them with a space.
如果您使用的是MultiLine文本框(从您的示例中可以看出),那么您无需搜索换行符并将其替换为空格。
You could simply use the Linesproperty where every line is stored separated from the other and then use the string Join method to create a single line string
您可以简单地使用Lines属性,其中每一行都与另一行分开存储,然后使用字符串 Join 方法创建单行字符串
Dim singleLine = string.Join(" ", myTextBox.Lines)
Of course if you are just interested to know if there is a newline character then just check the Length property of the Lines array
当然,如果您只是想知道是否有换行符,那么只需检查 Lines 数组的 Length 属性
回答by SeraM
vbCrLf actually refers to two characters, a carriage return (13) and a line feed (10).
vbCrLf 实际上是指两个字符,一个回车 (13) 和一个换行 (10)。
I would search and replace each separately. It isn't strictly necessary (as replace will work on the two characters at once), but can catch instances in which the user cut and pasted information, instead of typing directly into the text box.
我会分别搜索和替换每个。这不是绝对必要的(因为 replace 将同时处理两个字符),但可以捕获用户剪切和粘贴信息的实例,而不是直接在文本框中键入。
Text = Text.Replace(vbCr, " ")
Text = Text.Replace(vbLf, " ")
or
或者
Text = Text.Replace(vbCr, " ").Replace(vbLf, " ")
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.constants.vbcrlf(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.constants.vbcrlf(v=vs.110).aspx
回答by user700390
The Replace()function will indeed properly detect and replace all occurrences of the target with the replacement - it does not matter whether there are leading or trailing spaces.
该Replace()函数确实会正确地检测和替换目标的所有匹配项 - 是否有前导空格或尾随空格都没有关系。
However, please consider that Stringobjects are immutable and cannot be changed after they have been instantiated. Therefore, Replace()does not modify the existing object but rather returns a newstring as its result.
但是,请注意String对象是不可变的,并且在实例化后无法更改。因此,Replace()不会修改现有对象,而是返回一个新字符串作为其结果。
To actually see the results of the function call, you need to do something along these lines:
要实际查看函数调用的结果,您需要执行以下操作:
newString = Text.Replace(vbCrLf, " ")
newString = Text.Replace(vbCrLf, " ")
回答by OSKM
I spent quite some time to resolve exactly this problem, the solution I came across was:
我花了相当多的时间来解决这个问题,我遇到的解决方案是:
text = text.Replace(" ", ControlChars.CrLf)
text = text.Replace(" ", ControlChars.CrLf)
Sorry cant remember where I found the solution but if I do remember it I will post the link here.
对不起,我不记得我在哪里找到了解决方案,但如果我记得它,我会在这里发布链接。

