Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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
0
igfp
Asked:
igfp
  • 2
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now