Removal of excess whitespace

Asked

Viewed 930 times

6

How to remove white spaces inside a string?

It would be in the sense of removing spaces from a text string, with the exception of simple spaces between words. As the Excel "tidy" function.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

9

The function AllTrim() solves most cases. If you want to delete in the middle:

Function MiddleTrim(cText)
    Local cClean := ""
    Local cLast := " "
    Local nLen := Len(cText)
    Local i
    Local cChar
    For i := 1 To nLen
        cChar := Substr(cText, i, 1)
        If cChar != " "
            cClean += cChar
        ElseIf cLast != " "
            cClean += " "
        EndIf
        cLast := cChar
    Next
    If Substr(cClean, Len(cClean), 1) == " "
        cClean := Substr(cClean, 1, Len(cClean) - 1)
    EndIf
Return cClean

Also put on the Github for future reference.

This way it reads character by character identifying if there was already a space before, only copies the character if it is not space or if it is the space immediately following a non-space, that is, there is only one space. As there can be one last space at the end, it clears too. Traversing character by character is what the StrTran() and the AllTrim() would, so has chance to be more efficient this way by walking only once.

In Harbour maybe not because it would run in C very efficiently. I don’t know how it is today, but ADVPL had very inefficient implementation in many things, so much so that some common features in Harbour or even Clipper recommend avoiding use.

  • To eliminate the spaces in the middle, I believe that the StrTran(cText, " ") be simpler

  • Then take out all the spaces.

  • Ok, but easier to check if there is a space at the end and/or start, use strtran, and then add if you need to. There runs directly on the application server, and not via advpl.

  • Try to do so. It does not give the desired result in the question. It’s been a while since I’ve touched the platform, I don’t know how this application server issue is going and so I didn’t understand what you meant by that, can you explain to me?

6

An alternative:

Function MiddleTrim(cText)
    While "  " $ cText
        cText = StrTran(cText, "  ", " ")
    End
    Return AllTrim(cText)

Remembering that although shorter than the @Maniero response (which already received my +1), it is not necessarily more performative, as it can go through the string several times.

Points of interest:

  • StrTran(cText, " ", " ") exchange occurrences of two spaces for only one. But since the function is not recursive, you need a loop;

  • While " " $ cText loops while " " is still in cText;

  • Finally the AllTrim removes the spaces from the "tips" of the string.

Note:

Based on Harbour’s syntax, if there is any difference in Advpl it will probably just be an adjustment in the While .. End. What is relevant here is logic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.