How to determine if a string has a carriage return using the String.Contain function using the ascii character?

In C #, how to determine if a string has a carriage return using the String.Contains function? Return ascii for carriage - 13.

Chr(13) is how carriage returns are presented in Visual Basic. How is carriage return in C # represented using the ascii character rather than "\r" ?

 if (word.Contains(Chr(13)) { . . . } 
+8
source share
7 answers

Since you are declaring that you do not want to use \r , then you can apply an integer to char :

 if (word.Contains((char)13)) { ... } 
+16
source
 if (word.Contains(Environment.NewLine)) { } 
+15
source

You can enter a char value using single quotes

 var s = "hello\r"; if (s.Contains('\r')) { } 

If you find it easier to read, you can cast 13 to char

 var s = "hello\r"; if (s.Contains((char)13)) { } 
+5
source

This is valid in all versions of .NET:

 if (word.Contains("\r")) { ... } 

This is true only from .NET 3.5:

 if (word.Contains('\r')) { ... } 
+1
source

Convert.Char(byte asciiValue) creates a char from any integer; So

 if (word.Contains(Convert.Char(13)) 

must do the job.

+1
source
 s.Contains('\x0D'); 

characters are single quotes;

What is wrong with using \ r?

+1
source

I'm sure you can do this with a regex, but if you're stupid like me, this extension method is a good way:

 public static bool HasLineBreaks(this string expression) { return expression.Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None).Length > 1; } 
0
source

Source: https://habr.com/ru/post/901324/


All Articles