Solved

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

Posted on 2006-06-20
10
228 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
[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
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
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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

705 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