Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Hi,
I am reading a Microsoft Office Word text from a vb.net app, and then storing the formatted string in another ans file. Somewhere in this file I am finding 4 unreadable characters. I went in debug mode and I found out that the Asc code of each of these characters is 32 !!
Normally this is the space code, although I used:
VB
rettext = Replace(rettext, " ", "" )
and
VB
rettext = Replace(rettext, Asc(32), "" )
and
VB
rettext = Trim(rettext)

but still the rettext variable is not changed and have the same length!!

Anyone have an idea what to do ??
Thx in advance.
Posted
Updated 24-Oct-13 4:53am
v2
Comments
saguptamca 24-Oct-13 10:19am    
I understand the issue. But can u Pls provide original sample code to solve the issue.
saleem_deek 24-Oct-13 10:52am    
l = l.Replace(Chr(32), "")
l = l.Replace(" ", "")
l = Trim(l)
ledtech3 24-Oct-13 10:49am    
Without more code to view where you are checking the lenth.
You may be only returning the original variable length not the trimmed length.
you may try to asign the output of the trim /replace function to another variable and check the length of that.
saleem_deek 24-Oct-13 10:52am    
this is my code:
l = l.Replace(Chr(32), "")
l = l.Replace(" ", "")
l = Trim(l)
ledtech3 24-Oct-13 11:04am    
you are using all 3 at the same time?
I would think it should be throwing some kind of error.

I think part of the problem is you are baisicly resseting the output to what the input was.
without being able to step thru it see what it is doing.
use a different variable name for the output as you do for the input and see what happens.

1 solution

To remove blank spaces from a string, use:
C#
rettext = rettext.Replace(" ", string.Empty);


Please see: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^].

(Always use string.Empty instead of "", by a number of reasons.) It should work. If the string is not changes (are you 100% sure?), it simply means that you don't have blank space characters in your input string. Note that many other characters may only look similar to the blank space, but be something else: Em space, En space, Non-breakable space, Tab, and more. I hope you typed " " using blank space, not something else. Look at your input string carefully.

And of course, don't try to use ASCII in .NET, please see my comment to the question. More exactly, you can use it, but only if you can, by some special reasons, guarantee that no input will be beyond ASCII. And learn how Unicode works, it is less trivial than many think.

—SA
 
Share this answer
 
v2
Comments
ledtech3 24-Oct-13 11:50am    
That worked fine in my test sample.
Just had to remove the semicolon at the end and "Dim" it up for VB.Net
Thanks for another way to do it.
Sergey Alexandrovich Kryukov 24-Oct-13 13:11pm    
My pleasure.
Good luck, call again.
—SA
saleem_deek 24-Oct-13 11:50am    
I will learn about Unicode, I'm sure this is the issue here.
Thanks a lot for answering :).
Sergey Alexandrovich Kryukov 24-Oct-13 13:12pm    
Certainly you will need it. In case of doubt, you are very welcome to ask new questions. :-)
—SA
saleem_deek 24-Oct-13 11:51am    
Especially that the source of this string is a bookmark in a Word file.

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