Replace Text

Hi everyone, I'm using this to replace every /n in my textbox to /n/n

Replace(textbox1.text, "\n", "\n\n")

but don't want the first neither the last to be changed, How do I do this?
Suppose I've got: dsafjkoasdfnas /n dsajfsda /n dfas /n bebebe

I want: dsafjkoasdfnas /n dsajfsda /n/n dfas /n bebebe
another problem: I have no idea how much chars will be before the first neither after the last.

thank you everyone
igfpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trkcorpCommented:
Dim x As String
Dim y As String
Dim lPos1 As Long
Dim lPos2 As Long

x = "dsafjkoasdfnas /n dsajfsda /n dfas /n bebebe"
lPos1 = InStr(1, x, "/n")
lPos2 = InStrRev(x, "/n")
Debug.Print x
y = Replace(Mid$(x, lPos1 + 1, (lPos2 - lPos1) - 1), "/n", "/n/n")
Debug.Print y
x = Mid$(x, 1, lPos1) & y & Mid$(x, (lPos1 - 1) + Len(y))
Debug.Print x  

You should end up with "dsafjkoasdfnas /n dsajfsda /n/n dfas /n bebebe"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trkcorpCommented:
Of course the above ASSUMES there is always 2 "/n" strings in the string...
0
cyberknetCommented:
Here is some code that assumes absolutely nothing. It will search some text for text you specify, and replace it with text you specify. It will only replace occurances between the first and last occurance (if there are any occurances). I think that this code is a little bit better than the code above because it is user configurable, and does not assume anything about the string being processed... it will probably suit you better.

I hope this helps more.

Usage:
strResult = ReplaceMiddleStrings(Original String, String To Find, String To Replace With

Example:
strResult = ReplaceMiddleStrings("\nHello\nWorld\n", "\n", "\n\n")

Function:
Private Function ReplaceMiddleStrings(ByVal strText As String, ByVal strFind As String, ByVal strReplace As String)
Dim lngFirstFind As Long, lngLastFind As Long, lngPosition As Long

    lngFirstFind = InStr(1, strText, strFind)
    If lngFirstFind > 0 Then
        lngLastFind = InStrRev(strText, strFind)
        If lngLastFind > lngFirstFind Then
            lngPosition = InStr(lngFirstFind + Len(strFind), strText, strFind)
   
            While lngPosition < lngLastFind
               
                strText = Left(strText, lngPosition - 1) & Replace(strText, strFind, strReplace, lngPosition, 1)
                lngPosition = InStr(lngPosition + Len(strReplace), strText, strFind)
            Wend
        End If
    End If
    ReplaceMiddleStrings = strText
End Function
0
igfpAuthor Commented:
I got it to work modifiing trkcorp code. It's working fine now. Actually there are always 2 /n for sure and i used a different implementation that didn't use X and Y string. I got it to do just what I wanted the same. The idea of getting a psition is great! Thanks!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.