Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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 "

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


This is the value of strLine at the end:
Someplace, Someplace & Someplace Water District                                                                                                            Client Number: 000000


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?
Posted
Updated 27-Apr-11 7:52am
v3

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")
 
Share this answer
 
...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:
VB
Dim myRegex As New System.Text.RegularExpressions.Regex("\s+")
strNoWhiteSpace = myRegex.Replace(strLine, "\s+", " ")
 
Share this answer
 
v2
Comments
MacRaider4 27-Apr-11 13:41pm    
The problem is I don't know what the first character is going to be, it could be any letter of the alphabet. The only upside is I do know that it's going to be a alpha character. I have also tried just plain .Trim and that doesn't work either.
Kschuler 27-Apr-11 13:44pm    
Okay...but what are you trying to do? What is the end result you are looking for? Does your string include the double quotes and that's why trim isn't working? Or are you only trying to remove one space? Please update your question with more information about what you are starting with, what you want the end result to be, and what you are getting with the current code you are using.
MacRaider4 27-Apr-11 13:49pm    
Just did...
Kschuler 27-Apr-11 13:53pm    
Maybe update one more time and include what the value of strLine is before you run that code.
MacRaider4 27-Apr-11 13:58pm    
It's the same value, just has a few more spaces in it... strLine1 is correct and ends with the last character of the first group, it's strLine2 that is the problem for some reason.
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.
 
Share this answer
 
Comments
MacRaider4 27-Apr-11 14:01pm    
I checked the txt file and there are no tabs in it...
#realJSOP 27-Apr-11 14:43pm    
Here's a novel idea - use the debugger and inspect the string contents *IN THE PROGRAM*.
MacRaider4 27-Apr-11 15:06pm    
Those results would be found above....
Greg Osborne 27-Apr-11 14:26pm    
Could the character be a null character (ascii 0)? Some editors change this to space, others display it as a box, some ignore it so you wouldn't even know it's there. are you sure it's specifically ascii 32 (chr(32))?
MacRaider4 27-Apr-11 14:30pm    
I've looked the the file in both notepad and programmer's file editor and the best I can tell they are just plain old spaces which is why this doesn't make sense.
You could use a LINQ query to easily pull the alpha characters from a string

Dim myString As String = "abC123X!!"
Dim q = From a In myString.ToCharArray() _
        Where Char.IsLetter(a) _
        Select a
Dim anewString As String = Nothing
If q.Count() > 0 Then
    anewString = Convert.ToString(q.ToArray())
End If


If you are looking for just uppercase characters, change char.IsLetter to char.IsUpper.
 
Share this answer
 
Comments
MacRaider4 27-Apr-11 14:02pm    
good thought and I thought of something similar like the regex idea, but stepping through that code 100+ times for countless lines would take way too much time when you are looking at 66 lines per page time who knows how many pages.
Bare with me as I make some assumptions here...

Is the text file the same format for every line? If so then I am assuming you have some sort of extract from a database in fixed-field format. If this is the case, do you know the field definitions of the source? (i.e., field 1 is 126 characters, field 2 is 16 characters, field 3 is 4 characters, etc.). If you have the field sizes, then all you would need to do is

Dim field1 as string = source.SubString(0,126).Trim()
Dim field2 as string = source.SubString(127,16).Trim() '126+1
Dim field3 as string = source.SubString(144,4).Trim() '126+16+1
'etc...


You'd need to do this for each line. Does this help?
 
Share this answer
 
v4
Comments
MacRaider4 27-Apr-11 14:23pm    
That would be super if it were true, but it's not... I've looked at 15 different text files at this one line and the range is anywhere between 115 and 142 blank spaces between the two groups of strings. Otherwise I could use something along the lines of strLine2 = source.Substring(source.indexof(" ") + 126) of which is how I first had it and when I got the the second letter, I had a problem.
MacRaider4 27-Apr-11 14:32pm    
I just love when a project is going great and you run full speed into the "progress wall"...
Well, this is technically a solution, the data I was given to work with was wrong... So with the "correct" data, this works fine. Thanks all for the help though in trying to get this to work. You have given me some ideas to try going forward...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900