Solved

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

Posted on 2006-06-20
10
223 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

17 Experts available now in Live!

Get 1:1 Help Now