Cannot convert string to IEnumerable<string> [duplicate] - c#

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 1 year ago.
I need to convert string to IEnumerable<string> by every newline character.
I just want the same functionality as File.ReadLines without loading any file, rather taking it from a previously loaded string.

String.Split can split your string and give you string[]
Usage
string someString = ...;
string[] lines = someString.Split('\n');
IEnumerable<string> linesAsEnumerable = lines;
DoSomethingWithLines(linesAsEnumerable);

You could split it according to the newline character:
String[] lines = fileContenets.Split('\n');

Related

How to split a string array using char as delimiter [duplicate]

This question already has answers here:
How to split a string by a specific character? [duplicate]
(6 answers)
Closed 10 months ago.
I want to add ' char as delimiter to my string array but I have error telling conversion string to char impossible.
I see this How to split a string by a specific character?
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split("'").ToString();
The issues is that you have the .ToString() after your statement.
Change it to this:
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split('\'');
The reason why this code doesn't work:
string[] splitTest = ineedtosplitthis.Split("'").ToString();
Is because you are converting it to a string via ToString();, while assigning it to a string[].
Split(); already returns a string[], so there is no need to convert it.
Hope this explanation helped :)

Split a string after reading square brackets in c# [duplicate]

This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);

Split by two or more spaces [duplicate]

This question already has answers here:
Split string separated by multiple spaces, ignoring single spaces
(3 answers)
Closed 7 years ago.
Lets say I have a string like this:
"15 Lore ipsum"
Is there a way to split it so I got 3 strings after split ["15", "Lore", "Ipsum"]. So I want delimiter to be a space consisting of two or more spaces, I don't want one space to be delimiter...
You can use string.Split(char[], StringSplitOptions) method:
string s = "15 Lore ipsum";
string result = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
\s stands for "whitespace character".
http://www.w3schools.com/jsref/jsref_regexp_whitespace.asp
Try (.*?)\s+(.*?)\s+(.*?)$
https://regex101.com/r/oH8qY6/1

Split a string with slash [duplicate]

This question already has an answer here:
Splitting the array with / slash
(1 answer)
Closed 7 years ago.
I have a string which includes integers between slashes.
For example:
string myString = "/1//2//56//21/";
I need to take these integers and add them into a list. How can I split this string to its integers?
string myString = "/1//2//56//21/";
int[] arrayInt = Regex.Split(myString, "/+").Where(s => !String.IsNullOrWhiteSpace(s)).Select(Int32.Parse).ToArray();

C# - Split a string with spaces in between in multiple strings [duplicate]

This question already has answers here:
Best way to specify whitespace in a String.Split operation
(11 answers)
C# Splitting Strings on `#` character
(3 answers)
Closed 7 years ago.
I have a string which is a sentence. For example:
string sentence = "Example sentence";
How can I divide this string into multiple strings? So:
string one = "Example";
string two = "sentence";
This is a dupe but you are looking for string.Split (https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx) --
public class Program
{
public static void Main(string[] args)
{
string sentence = "Example sentence";
string[] array = sentence.Split(' ');
foreach (string val in array.Where(i => !string.IsNullOrEmpty(i)))
{
Console.WriteLine(val);
}
}
}
The .Where ensures empty strings are skipped.
This will work
string sentence = "Example sentence";
string [] sentenses = sentence.Split(' ');
string one = sentenses[0];
string two = sentenses[1];

Categories