?
Solved

Replace Text

Posted on 2003-03-20
4
Medium Priority
?
172 Views
Last Modified: 2012-05-04
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
Comment
Question by:igfp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
trkcorp earned 60 total points
ID: 8176724
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8176756
Of course the above ASSUMES there is always 2 "/n" strings in the string...
0
 
LVL 1

Expert Comment

by:cyberknet
ID: 8177527
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
 

Author Comment

by:igfp
ID: 8183654
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question