'Get the part of a string before new line symbol
I do want to get all the text before the first \r\n\ symbol. For example,
lalal lalal laldlasdaa daslda\r\n\Prefersaasda reanreainrea
I would need: lalal lalal laldlasdaa daslda. The above string can be empty, but it also may not contain any '\r\n\' symbol." How can I achieve this?
Solution 1:[1]
Hope that this is what you are looking for:
string inputStr = "lalal lalal laldlasdaa daslda\r\nPrefersaasda reanreainrea";
int newLineIndex = inputStr.IndexOf("\r\n");
if(newLineIndex != -1)
{
string outPutStr = inputStr.Substring(0, newLineIndex );
// Continue
}
else
{
// Display message no new line character
}
Checkout an example here
Solution 2:[2]
You can use Split like this
string mystring = @"lalal lalal laldlasdaa daslda\r\n\Prefersaasda reanreainrea";
string mylines = mystring.Split(new string[] { @"\r\n" }, StringSplitOptions.None)[0];
Whereas if the string is like
string mystring = "lalal lalal laldlasdaa daslda\r\n Prefersaasda reanreainrea";
string mylines = mystring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[0];
Environment.NewLine will also work with it. The with \r\n\P is making that string an invalid string thus \r\n P makes it a new line
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sujith karivelil |
| Solution 2 |

