Solved

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

Posted on 2006-06-20
10
224 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
10 Comments
 
LVL 20

Accepted Solution

by:
alainbryden earned 50 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

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 20

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 20

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now