In C#, how do I find whether a string has a carriage return by using the String.Contains
function?
The ascii for carriage return is 13.
Chr(13)
is how a carriage return is represented in Visual Basic. How is a carriage return represented in C# using its ascii character and not "\r"
?
if (word.Contains(Chr(13))
{
.
.
.
}
In ASCII and Unicode, the carriage return is defined as 13 (or hexadecimal 0D); it may also be seen as control+M or ^M. In the C programming language, and many other languages (including regular expression) influenced by it, \r denotes this character.
Open any text file and click on the pilcrow (¶) button. Notepad++ will show all of the characters with newline characters in either the CR and LF format. If it is a Windows EOL encoded file, the newline characters of CR LF will appear (\r\n). If the file is UNIX or Mac EOL encoded, then it will only show LF (\n).
Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.
A carriage return would do exactly that, return the print head carriage to the beginning of the line. A newline character would simple shift the roller to the next line without moving the print head.
Since you state that you don't want to use \r
then you can cast the integer to a char
:
if (word.Contains((char)13)) { ... }
if (word.Contains(Environment.NewLine)) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With