Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding all the "\n" is a string and removing it

Posted on 2006-06-20
10
Medium Priority
?
232 Views
Last Modified: 2010-04-07
Hello,

I wanted to know how can is there any way to find out if there is "\n" (or more then 1) in my string  and if so remove it.

I tired the Replace function but i wasn't succeesfull

YoniS


0
Comment
Question by:Cyber-EE
7 Comments
 
LVL 21

Accepted Solution

by:
alainbryden earned 200 total points
ID: 16941482
how did you use the replace function? '\n' isn't a character in the visual basic language.

What you need to do is a replace function on vbCrLf (which is the line break for windows) or vbLf, for unix, or Environment.NewLine if what you care about is replacing the system "Enter" key strokes. This can also, I think, be simulated using the vbNewLine character.

So the code should be:

YourText = (assign however you do it)
OneLineText = Replace$(YourText, vbNewLine, ",") '(for example)

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16941487
u could try using the replace command

newstring = replace(mystring, chr$(13), "")

0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16942314
// sInput = your_string_to_replace
// sCharToRemove = "\n"

Public Function RemoveChar(ByVal sInput As String, ByVal _
   sCharToRemove) As String
Dim iPos As Integer

iPos = InStr(sInput, sCharToRemove)
While iPos > 0
    iPos = InStr(sInput, sCharToRemove)
    If iPos > 0 Then sInput = Left(sInput, iPos - 1) & _
       Mid(sInput, iPos + 1)
Wend

RemoveChar = sInput

End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:alainbryden
ID: 16943199
kaliyugkaarjun, there are 5 things wrong with that function, aside from the fact that it can be emulated in one line using the replace function I stated earlier. See if you can find the 4 errors:

Public Function RemoveChar(ByVal sInput As String, _
                                           ByVal strToRemove As String) As String
    Dim iPos As Integer
    iPos = InStr(sInput, strToRemove)
    Do While iPos > 0
        sInput = Left$(sInput, iPos - 1) & Mid$(sInput, iPos + len(strToRemove) )
        iPos = InStr(sInput, strToRemove)
    Loop
    RemoveChar = sInput
End Function

1)One huge error is that the carriage return is often not a single character, but a two character combination, so your function would fail and garble the text as soon as the element to replace was longer than one character, which in this application is big.
2) While/wend is extremely outdated, and no longer has a use, it should not be used
3) Not using the $ character results in more memory consumption and time spent converting the returned Variant back in to a string.
4) You omitted the type declaration for the second String parameter
5) Your loop was malformed, you were resetting iPos at the beginning instead of the end of the loop, which made an if statement necessary when it should not be, and which made one loop occur at the end that did nothing. All this was uneccessary.

Rockiroads, even though you copied my equation and failed to realize that he had allready mentioned attempting the replace function, what you showed has two problems.
1) There is no $ character, again wasting memory and time.
2) chr$(13) is not a universal carriage return and would fail on most operating systems.

I hope I was helpful to everyone including Cyber-EE and the two other commenters.
0
 
LVL 21

Expert Comment

by:alainbryden
ID: 16943221
I appologise if it was not clear, but the function demonstrated above is the correct version of the attempt kaliyugkaarjun made. This is not even necessary however, as the Replace$() function should suffice.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16943572

Cyber-EE can you show us the code that you tried, and maybey a part of the string?
0
 
LVL 21

Expert Comment

by:alainbryden
ID: 16943631
I'm pretty sure the solution is up there, he just needs to come back to the thread and read it now.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month21 days, 7 hours left to enroll

805 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