Solved

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

Posted on 2006-06-20
10
225 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

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