字符串操作,修剪和替换不起作用 [英] String manipulation, trim and replace not working

查看:67
本文介绍了字符串操作,修剪和替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VB.net 2010
我试过strLine2 = strLine2.Trim(" ")strLine2 = strLine2.Replace(" ", "")都没有运气,我仍然在strLine2中得到类似于

Using VB.net 2010
I''ve tried strLine2 = strLine2.Trim(" ") and strLine2 = strLine2.Replace(" ", "") with no luck I''m still getting a value in strLine2 similar to

"                   Start of Text "


的值 有没有办法寻找大于64的ascii字符,我相信它是A(好的A实际上是65,因此大于64的部分).因此,类似于以下内容:


Is there a way to look for a ascii character greater than 64, which I believe is A (ok A is actually 65, thus the greater than 64 part). So something along the lines of:

strLine2 = strLine2.Substring(strLine2.IndexOf(''Ascii character > 64''))



在此先谢谢!!

好的,这是我的代码,非常简单:



Thanks in advance!!!

Ok this is the code I have, and it''s pretty simple:

strline1 = strLine.Substring(0, strLine.IndexOf("     "))
strLine2 = strLine.Substring(strline1.Length + 1)
strLine2 = strLine2.Trim()
strLine = strline1 + strLine2



这是结尾处strLine的值:



This is the value of strLine at the end:

Someplace, Someplace & Someplace Water District                                                                                                            Client Number: 000000



忘了解释这是为了什么,对不起...
这是读取文本文件并将其更改为Word doc的应用程序的一部分,问题在于文本文件的一行上可能有疯狂的空格,而这些文本永远无法容纳在word doc的一行中.因此,我必须能够将行中的文本分割开,并将其放到合适的位置.当前行在第一组字符的最后一个字符和第二组字符的开始之间有126个空格.



Forgot to explain what this is for, sorry...
This is part of a app that reads a text file and changes it over to a word doc, the problem is the text file can have a insane number of spaces on a line that will never fit onto one line in a word doc. So I have to be able to split up the text in the line and get it to where it will fit. The current line has 126 spaces between the last character in the first group of characters and the start of the second group of characters. Does this make more sense now?

推荐答案

不要在带有修饰符的空格中使用

Dim x As String =文本开始"
Dim y As String = x.Trim()
" y等于(文本开始")
Don''t use a space with trim

Dim x As String = " Start of Text "
Dim y As String = x.Trim()
'' y will equal ("Start of Text")


...您想要摆脱什么?只是正常的空间?
如果是这样,请尝试...
strLine2 = strLine2.Trim
您不需要(").如果不是,您到底想摆脱什么角色?

您可以通过以下方式找到字符的ascii值:

Asc(myChar)

如果您使用它来确保仅使用有效字符,则建议您研究 ^ ].

----编辑----------

我认为您需要看一下本文 [
...What are you trying to get rid of? Just normal spaces?
If so try...
strLine2 = strLine2.Trim
You don''t need the (" "). If not, exactly what character are you trying to get rid of?

You can find the ascii value of a character this way:

Asc(myChar)

If you are using this to make sure only valid characters are used, I''d suggest you research Regular Expressions[^].

---- Edit ----------

I think you need to take a look at this article[^].

It shows how to remove white space using regular expressions. I think it will give you some ideas on how to perform these tasks without having to worry about how many spaces there are or what index a word starts at. Here is the basic code to replace multiple spaces, carriage returns, and tabs with a single space:
Dim myRegex As New System.Text.RegularExpressions.Regex("\s+")
strNoWhiteSpace = myRegex.Replace(strLine, "\s+", " ")


string.Trim()会修剪所有前导和尾随空格(包括制表符),除非您将字符指定为"trim".我敢打赌,您领先的空格是一系列制表符,并且因为您要指定空格字符,所以不会对它们进行修剪.
string.Trim() trims all leading and trailing whitespace (including tab characters) unless you specify the character to "trim". I bet your leading whitespace is a series of tab characters, nd it''s not trimming those because you''re specifying the space character.


这篇关于字符串操作,修剪和替换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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