Remove Only Leading or Trailing Carriage Returns

Remove only leading or trailing carriage returns

Find the first character that is not CHAR(13) or CHAR(10) and subtract its position from the string's length.

LTRIM()

SELECT RIGHT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',@MyString)+1)

RTRIM()

SELECT LEFT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',REVERSE(@MyString))+1)

VBA Replace Only leading and trailing line breaks

Here is a trimCHAR function that works similarly to Excel's TRIM function, except you can specify the character to be TRIM'd.

It will remove all leading and trailing char, as well as any doubled char within the string (leaving a single char within the string, as TRIM does with spaces)

Function trimCHAR(ByVal S As String, char As String)
'similar to worksheet TRIM function except can specify character(s) to TRIM
Dim RE As Object
Dim I As Long

Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.multiLine = True

'need to do separately, otherwise multiple chars within will
'be removed
.Pattern = char & "*$"
S = .Replace(S, "") 'Remove extra chars at end of string
.Pattern = char & "*([^" & char & "]" & char & ")*"
S = .Replace(S, "$1") 'Remove extra chars at start of string or within
End With
trimCHAR = S

End Function

Original

Sample Image

Usage on worksheet: =trimCHAR(cell_ref,CHAR(10))

in a macro, you might use =trimChar(myStringVariable, vblf)

Result

Sample Image

Does the MySQL TRIM function not trim line breaks or carriage returns?

Trim() in MySQL only removes spaces.

I don't believe there is a built-in way to remove all kinds of trailing and leading whitespace in MySQL, unless you repeatedly use Trim().

I suggest you use another language to clean up your current data and simply make sure your inputs are sanitized from now on.

Using AWK or Sed how can I remove trailing carriage return and a line feeds before first txt

All you need is:

awk 'NF{f=1}f' file

Remove carriage return and space from a string

Try:

 t.replace(/[\n\r]+/g, '');

Then:

 t.replace(/\s{2,10}/g, ' ');

The 2nd one should get rid of more than 1 space

How to remove newlines from beginning and end of a string?

Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

String trimmedString = myString.trim();


Related Topics



Leave a reply



Submit