I'm making my filter such that an end user can use Notepad to directly edit the file so there's no telling where these characters might embed themselves -- could be other than at the start or end of the string. So removing them all does it. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Removing carriage return and new-line from the end of a string in c Ask Question. Asked 12 years, 8 months ago.
Active 1 year, 5 months ago. Viewed k times. Avik Avik 1, 7 7 gold badges 23 23 silver badges 30 30 bronze badges. Add a comment. Active Oldest Votes. RichieHindle RichieHindle k 45 45 gold badges silver badges bronze badges. Take note of the params keyword in the declaration of TrimEnd, it let's you pass multiple instances of a character instead of the array. TrimEnd System. I've been writing c since v1. Now you tell me about TrimEnd. TrimEnd - as the docs say: If trimChars is null or an empty array, Unicode white-space characters are removed instead.
TrimEnd — Stuart Wood. Show 2 more comments. This should work If you are using multiple platforms you are safer using this method. ToCharArray ; It will account for different newline and carriage-return characters. Alex Wiese Alex Wiese 7, 6 6 gold badges 43 43 silver badges 70 70 bronze badges. Thus we use the Right function to grab the two characters at the very end of the string and store these in the variable strEnd.
We can then check to see if strEnd is equal to vbCrLf. If it is, we use this line of code to grab all the characters except the two at the very end:. Remember when we used the Len function to determine how many characters are in the string? That will effectively chop the carriage return-linefeed off the end of the string.
We then reopen our text file — this time for writing — and replace the existing contents with the new string value. That will leave us with our original text file, minus the trailing carriage return-linefeed. Comments are closed. Scripting Forums. PowerShell Forums. PowerShell on TechCommunity. May 20th, You probably meant to replace them with nothing String. Empty, or "" : Note I am not using single quotes characters , rather double quotes for a string. I also just noticed that you are not setting val equal to the value that you're changing it to: val.
It does not change the existing variable, val's, value. The reason both of our examples would work for what we wanted them to do is because we are changing the returned string and saving the final result to val. If you wanted to remove the characters, then you should use my code. Thanks very much for your help. It seems the reason my code failed was because I did not update the existing variable val's value. The interesting result of this is that whilst it removes the offending CRLF characters in the text file, It also removes the end of line characters which one would expect except that in the source file my text editor SCITE only shows the offending CRLF scattered throught the file, and it does not show the end of line characters.
0コメント