Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find whether a string has a carriage return by using the String.Contain function using its ascii character?

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))  
{  
    .  
    .  
    .  
}  
like image 976
C N Avatar asked Nov 11 '11 21:11

C N


People also ask

What is carriage return in ASCII?

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.

How do you check carriage returns in a text file?

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).

How do you check if a string has any characters?

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.

What is difference between newline and carriage return?

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.


2 Answers

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)) { ... }
like image 195
Mark Byers Avatar answered Oct 04 '22 19:10

Mark Byers


if (word.Contains(Environment.NewLine)) { }
like image 37
Joao Avatar answered Oct 04 '22 18:10

Joao