字符串操作,修剪和替换不起作用

编程入门 行业动态 更新时间:2024-10-27 22:21:20
本文介绍了字符串操作,修剪和替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Using VB 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.

更多推荐

字符串操作,修剪和替换不起作用

本文发布于:2023-10-28 13:10:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不起作用   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!