How do I seperate a string by new lines? [duplicate] - c#

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 6 years ago.
For example,
string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info"
How do I get only the second line?

var secondLine = example.Split('\n')[1];

Use string splitting functions:
string[] split = example.Split('\n');
Each of the array items will be a line. Then you can access the one you want by the index.

You can use this code:
string[] text=example.split("\n");
text[1] will be the second line content.

Try this. Split to newline by using \r\n or \n
string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info";
string[] lines = example.Split(new string[]
{
"\r\n", "\n"
}
, StringSplitOptions.None);
foreach (var line in lines)
{
Console.WriteLine(line);
}

Related

How can I break up the text after the # I shared in the image? [duplicate]

This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 months ago.
I will create a parametric structure
I will take after each '#' with its extension.
For example here I need to get two texts after #
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
Full example on a .net 6.0 console:
using System.Text.RegularExpressions;
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
foreach (var x in results)
{
Console.WriteLine(x);
}
Output:
sdvdsv0..-+mk?/!|°¬ *$%&()oO
sdfv684618awer6816
Use "#" to match only a single # character.
Use "#+" to match at least once with the # character

How to Create a string array by spliting a string? [duplicate]

This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 6 years ago.
I am kinda stuck at the moment, i have a string of numbers which i dynamically get them from database, the range of numbers can be between 1 to 1 milion, something like this :
string str = "10000,68866225,77885525,3,787";
i need to create an array from it, i have tried this:
string[] strArr = { str.Replace(",", "").Split(',') };
but it doesnt work anyone has any solution i am all ours. Basically it needs to be like this:
string[] strArr = { "10000","68866225","77885525","3","787" };
Your attend:
string[] strArr = { str.Replace(",", "").Split(',') };
does not work because of two errors in the code:
1) You are removing all , before you are splitting at ,:
str.Replace(",", "")
So basically you are trying to split this string: "1000068866225778855253787" at each ,, which will result in an array containing only "1000068866225778855253787" because obviously there is no , to split on.
2) You are trying to assign an array to a string, because the Split() method already returns an array and you are trying to put this array into a field of string[] (because of the { } around your assignment) and a field of string[] is of type string and not an array.
To get your expected output you have to do the .Split(',') on the original string, which includes all the , on which you are splitting. So just remove the Replace() call and you will get your desired output:
var str = "10000,68866225,77885525,3,787";
var strArr = str.Split(',');
Try this:
string[] strArr = str.Split(',');
The split method already returns an array. Just take it out of the squiggly brackets.
string[] strArr = str.Split(',');
Edit: Sorry forgot to take out the .replace()
The method string.Split() already returns an array.
string[] strArr =yourstring.Split(',');
This will return an array with possible split counts , for example
"10000,68866225,77885525,3,787" will give array with the size of 4.

Splitting string with dots and spaces [duplicate]

This question already has answers here:
Regex split string but keep separators
(2 answers)
Closed 7 years ago.
Let's say I have the following string: "This is a test. Haha.". I want to split it so that it becomes these there lines:
Hey.
This is a test.
Haha.
(Note that the space after the dot is preserved).
I tried to split the string using the Split method, and split by the dot, but it returns 3 new strings with the space before the string, and it removes the dots. I want to keep the space after the dot and keep the space.
How can I achieve this?
EDIT: I found a workaround, but I'm sure there's a simpler way:
string a = "Hey. This is a test. Haha.";
string[] splitted = a.Split('.');
foreach(string b in splitted)
{
if (b.Length < 3)
{
continue;
}
string f = b.Remove(0, 1);
Console.WriteLine(f + ". ");
}
I can't test this but due to the post of Darin Dimitrov :
string input = "Hey. This is a test. Haha.";
string result = input.Replace(". ", ".\n");

How to get parts of a string [duplicate]

This question already has answers here:
How do I convert a string into an array?
(7 answers)
Closed 9 years ago.
I have this string:
string var = "x1,x2,x3,x4";
I want to get x1, x2, x3, x4 separately in four different strings.
This is a basic split
string yourString = "x1,x2,x3,x4";
string[] parts = yourString.Split(',');
foreach(string s in parts)
Console.WriteLine(s);
I renamed the original variable into yourString. While it seems that it is accepted in LinqPad as a valid identifier I think that it is very confusing using this name because is reserved
Split it up.
var tokens = someString.Split(',');
Now you can access them by their index:
var firstWord = tokens[0];
String.Split
var foo = "x1,x2,x3,x4";
var result = foo.Split(',');
There's also the "opposite: String.Join
Console.WriteLine(String.Join('*', result));
Will output: x1*x2*x3*x3
To separate values with comma between them, you can use String.Split() function.
string[] words = var.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}

C# - Split string by lines? [duplicate]

This question already has answers here:
Best way to split string into lines
(12 answers)
Closed 9 years ago.
Let's say, for example, I have the following string downloaded from a .txt file in the web.
line1
line2
line3
How can I split the whole string by lines, so I can use splitted[0] to get line1, splitted[1] to get line 2, etc..? Thanks!
Can I use?
string[] tokens = Regex.Split(input, #"\r?\n|\r");
Thanks
Use File.ReadAllLines to get the string[] with all lines:
string[] allLines = File.ReadAllLines(path);
string line10 = allLines[9]; // exception if there are less
string line100 = allLines.ElementAtOrDefault(99); // null if there are less
If you already have a string you can use String.Split with Environment.NewLine
string[] textLines = text.Split(new[]{ Environment.NewLine }, StringSplitOptions.None);
Use this:
var result = Regex.Split(text, "\r\n|\r|\n");
as indicated here: Best way to split string into lines
If you are downloading a file, then open it and ReadAllLines
var f= File.ReadAllLines(filPath)
ReadAllLines returns string[].

Categories

Resources