This question already has answers here:
split strings into many strings by newline?
(4 answers)
Closed 2 years ago.
I want to parse this string: "2.8\r\n2.52\r\n" to just get 2.8 and 2.52. How can I go about doing something like this? I tried to using .Split() but I'm not sure what to pass as parameters to get what I am looking for.
String.Split should work fine:
string[] res = "2.8\r\n2.52\r\n".Split("\r\n", StringSplitOptions.RemoveEmptyEntries); // array with {"2.8", "2.52"}
You need to split by Environment.NewLine which in here would be \r\n
var result = "2.8\r\n2.52\r\n".Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
Edit:
Environment.NewLine depends on your environment as you can guess so to be on the safe side you can also use the exact string (\r\n) as a delimiter.
Related
This question already has answers here:
Split string using backslash
(3 answers)
Closed 4 years ago.
I'm looking for the best way to extract the computer name from a predictably formatted string. The string will always be in this format:
C:\\Folder1\\Folder2\\NOOBCOMPUTER\\...
If there is a way to extract the contents of a string between the third pair of backslashes and the fourth pair, that should work.
Though I have no idea where to begin with the regex to achieve that, nor do I know if regex is the most "foolproof" way of going about this in C#.
You can split the string and then inspect each element.
string [] s = yourstring.Split("\\");
string final = s[3];
This question already has answers here:
string.split - by multiple character delimiter
(6 answers)
Closed 6 years ago.
Hey guys I have a quick question. I am trying to parse a single string into multiple strings using a keyword. I can currently find a set of code that allows me to parse the single string but I need to store each of the new strings as a new variable/item.
string full = "Hey//Please//Help";
parse into:
first string- Hey
Second string- Please
third string- Help
So for example I would like to manipulate the first string, Hey, by itself. Please let me know if any more explanation is necessary.
Simple String.Split:
string[] results = full.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
foreach(string str in results)
{
Console.WriteLine(str);
}
Result:
Hey
Please
Help
This question already has answers here:
Trim last character from a string
(15 answers)
Closed 8 years ago.
I have string like below.
string s="this is item1,item2,item3,,, ,, ,";
now i want to remove (,) from right side of string.
Thanks in advance
i have tried
string.Replace(",", "");
and
string.TrimEnd("'");
but not working
Try this:
s = s.TrimEnd(',', ' ');
I think the problem in your code is that you do not assign the result of your replacement to any variable. And also your solution would remove all , from the string, not only those on the right side.
Try string.TrimEnd():
s= s.TrimEnd(',',' ');
This question already has answers here:
C# string replacement , not working
(6 answers)
Closed 8 years ago.
I have a string like
mukesh "salaria" engineer
how do i replace " with blank
like i want output as
mukesh salaria engineer
I already tried, str.replace("\"",string.empty);
but it doesn't working for me.
It works but must type this: str = str.replace(...)
string.Replace is a pure method(no side-effects). If you do not assign str.replace("\"",string.empty); to anything, then that statement does not make any change in the state of your object(in other words writing that line of code is equal to not writing it at all.).
Use str = str.Replace(...);
You must use the Replace method and get the string back from it. Your code must be:
str = str.Replace("\"", string.Empty);
msdn :
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
try this
str.Replace('\"',' ');
This question already has answers here:
string.split - by multiple character delimiter
(6 answers)
Closed 7 years ago.
The result of doing
var b = "asfsadefbweabgggggggggggg".Split("ab".ToCharArray());
is a list of 6 strings while I want to split the array in "asfsadefbwe" and "gggggggggggg". Is there any way/method to properly do that (with C#)?
PS: I'll use a string which has some data separate by "\r\n" secuences.
string[] list = b.Split(new string[] { "ab" }, StringSplitOptions.None);
Use another overload, one that doesn't split on individual characters:
"asfsadefbweabgggggggggggg".Split(new [] {"ab" }, StringSplitOptions.None)
Are your substrings always the same length? If so, use String.Substring.