This question already has answers here:
Does any one know of a faster method to do String.Split()?
(14 answers)
Closed 9 years ago.
The string looks like this:
"c,c,c,c,c,c\r\nc,c,c,c,c,c\r\n.....c,c,c,c,c\r\n"
This line works:
IEnumerable<string[]> lineFields = File.ReadAllLines(sFile).Select(line => line.Split(','));
List<string[]> lLines = lineFields.ToList();
But let's say I'm not reading from a file, and instead of it I have the string i described before.
What's the fastest (I refer to preformance) way to convert it to a List<> of string[] that looks like
List<string[]> lLines = [ [c,c,c,c,c] , [c,c,c,c,c] , ... [c,c,c,c,c] ]
Thanks.
Something like this should work:
var list = "c,c,c,c,c,c\r\nc,c,c,c,c,c\r\n.....c,c,c,c,c\r\n"
.Split('\n')
.Select(s => s.Trim().Split(','));
Try something like this:
// replace \r\n to ; and split it... it will be your lines
var lines = text.replace("\r\n", ";").Split(';');
// split every item of the line arrays by , and get an new array to each item
List<string[]> arrays = lines.Select(x => x.Split(',')).ToList();
try this
string combindedString = string.Join( ",", myList );
Related
This question already has answers here:
c# split string and remove empty string
(6 answers)
Closed 3 years ago.
I have the following problem with this .NET C# application.
I have this string array:
string[] uorsList = uors.Split(';');
Sometimes this array contains an element corresponding to the empty string ("").
What is a smart way to remove all the element that are empty string from this uorsList array?
You can use LINQ to filter out the empty entires:
using System.Linq;
...
string[] uorsList = uors.Split(';');
var filtered = uorsList.Where(s=> !string.IsNullOrWhiteSpace(s)).ToArray();
EDIT:
As pointed out in comments, when using string.Split the following is a better option as the empty entries will never make it into the array:
string[] uorsList = uors.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
These are some ways to do it:
string uors = ";bla;bla;";
string[] uorsList = uors.Split(';').Where(x => string.IsNullOrEmpty(x) == false).ToArray();
string uors = ";bla;bla;";
string[] uorsList = uors.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string uors = ";bla;bla;";
List<string> uorsList = uors.Split(';').ToList();
uorsList.RemoveAll(x => string.IsNullOrEmpty(x));
You can try the following :
string[] uorsList = uors.Split(';').Where(s => s != string.IsNullOrWhiteSpace).ToArray();
This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 5 years ago.
Here is some basic simple code, where I want to process a string. Due to my requirements I have to replace a . with a , so that the further string to double parsing will work. For some reason the .s don't get replaced.
Sample code:
string[] pointArray = "3066.4735709236,4659.65039509825".Trim().Split(',');
foreach (var point in pointArray)
{
point.Replace('.',',');
}
//just checking for `.` in those strings
//a MessageBox pops up, because there are still `.` in the strings
Array.ForEach(pointArray, foo => { foo.Contains('.'); MessageBox.Show("has been not replaced"); });
What do I overlook?
string[] pointArray = "3066.4735709236,4659.65039509825".Trim().Split(',');
for (int i = 0; i < pointArray.Length; i++)
{
pointArray[i] = pointArray[i].Replace('.',',');
}
String are immutable, you have to set the value.
(Just a note, you have to use a for loop, because foreach doesn't allow item ti be modified.)
You can convert your source string to doubles array with linq:
var srcString = "3066.4735709236,4659.65039509825";
var doubles = srcString
.Trim()
.Split(',')
.SelectMany(s => s.Split('.').Select(double.Parse))
.ToArray();
This code will split your string by , then by . and convert each substring to double
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I want to separate a string and put it into an array.
Example:
id = 12,32,544,877,136,987
arraylist: [0]-->12
[1]-->32
[2]--544
[3]-->877
[4]-->136
[5]-->987
How to do that?
If your id var is a String, you can use the Split method:
id.Split(',')
Try:
string[] arraylist = id.Split(',');
Can do something like this in java :
ArrayList<String> idList = new ArrayList <String>();
String id = "12,32,544,877,136,987";
String idArr[] = id.split(",");
for(String idVal: idArr){
idList.add(idVal);
}
This question already has answers here:
Split String in C#
(9 answers)
Closed 7 years ago.
I've a string like this
cscript "E:\Data\System Test Performance\Some Tool\script.vbs" "AP_TEST" %%PARM1
I'm splitting above string like below
cmd.Split(' ')
Expected:
cscript
"E:\Data\System Test Performance\Some Tool\script.vbs"
"AP_TEST"
%%PARM1
But Actual results
There is a space in the string so your result is as expected. Try splitting on the quote instead:
var str = #"cscript ""E:\Data\System Test Performance\Some Tool\script.vbs"" ""AP_TEST"" %%PARM1";
str.Split('"').Select (s => s.Trim()).Where (s => !string.IsNullOrEmpty(s));
You need to write your own split function which supports text qualifiers
Check answer here Split String in C#
Or this article http://www.codeproject.com/Articles/15361/Split-Function-that-Supports-Text-Qualifiers
This might do the trick for you
string[] newinp = Regex.Split(inp, "(?=\")").Where(x => !string.IsNullOrEmpty(x)).ToArray();
There are so many spaces in your E:\Data\System Test Performance\Some Tool\script.vbs (file location) thats why you're getting the wrong array.
You may do two things
1) Make directory which doesn't contains spaces
2) Modify code
string[] final=new string[4];
final[0]=cmdLinesplit[0];
final[2]=cmdLinesplit[cmdLinesplit.Length-2];
final[3]=cmdLinesplit[cmdLinesplit.Length-1];
for(int i=1;i< cmdLinesplit.Length-2;i++)
{
final[1] +=cmdLinesplit[i]+" ";
}
final[1].Trim();
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Split string, convert ToList<int>() in one line
Convert comma separated string of ints to int array
I have a string like:
string test = "1,2,3,4";
Is there any easier way (syntactically) to convert it to a List<int> equivalent to something like this:
string[] testsplit = test.Split(',');
List<int> intTest = new List<int>();
foreach(string s in testsplit)
intTest.Add(int.Parse(s));
You can throw LINQ at it:
List<int> intTest = test.Split(',').Select(int.Parse).ToList();
It first splits the string, then parses each part(returning an IEnumerable<int>) and finally constructs a list from the integer sequence.
var result = test.Split(',').Select(x => int.Parse(x));
Or, if you really want a List<int> (rather than just any IEnumerable<int>), append a .ToList().
test.Split(',').Select(x => int.Parse(x)).ToList()
Linq can make it a bit cleaner:
var intTest = test.Split(',').Select(s=>int.Parse(s)).ToList();