Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Manipulating String

Status
Not open for further replies.

txgeekgirl1

Programmer
Sep 10, 2009
85
US
I have a string stored in an array that feeds an electronic signature device and then later needs to feed an iTextSharp PDF doc. It has to have the spacing to appear correctly on the ESig device but I want to strip it for the report.

"Were your HIPAA Privacy Rights explained and were you given a copy of them?"

Code:
MyQ = strQs(i, 1)
While InStr(MyQ, " ") > 0
Replace(MyQ, " ", " ")
End While
MsgBox(MyQ)

I was hoping something like this would work. In VFP I could use Transtr function - VB doesn't have that.
 
I don't get the reason for the loop as even in vb replace is an all or nothing thing. You can still use replace, but the more vb way would be:

MyQ.Replace(" ", " ")

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Because if you have 12 spaces, replacing all the double spaces with single spaces would still leave you with 6 spaces, hence the loop to make sure all spaces are single.

Dim myq As String = strQs(i, 1)
While myq.Contains(" ")
myq.Replace(" ", " ")
End While

~Ben
Occasional sparks in a darkened room
 

That's what I would use:
Code:
While Strings.InStr(MyQ, "  ") > 0[green]   '2 Spaces[/green]
    MyQ = Strings.Replace(MyQ, "  ", " ")[green]   '2 Spaces with 1 Space[/green]
End While
In my Replace I say: replace any 2 spaces with just one space, and do it as long as you have 2 spaces anywhere in the string MyQ

If you would have 3 spaces in the row, Replace takes first 2 and and puts just 1 in their place, so you end up with 1+1=2 spaces, so you need to replace those 2 spaces (againa) with just 1



Have fun.

---- Andy
 
You lost me at first, but I see what you are saying now. Yes what you have is what you want.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Here is what I used and it works great.

While MyQ.Contains(" ")
MyQ = MyQ.Replace(" ", " ")
End While

And you are correct - it has to be looped because some lines have up to 7 spaces and 1 line actually has 3 different multi-spaced breaks so that the Esig device captures it without breaking a word.
 
Or ...
Code:
Imports System.Text.RegularExpressions

...

MyQ = Regex.Replace(MyQ, "\s{2,}", " ")
\s refers to any white space character
{2,} say to look for two or more of these white space characters
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top