This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 4 years ago.
I've seen similar posts, but can't find anything that gives me an answer for my case
string[] entries = File.ReadAllLines();
var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(" ")[1]));
foreach (var entry in orderedEntries.Take(5))
{
Console.WriteLine(entry);
}
The error seems to be with this line:
var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(" ")[1]));
It says the it cannot convert from "string" to "char" which I'm assuming it means that it can only split by a character, is there a way I can change this to allow for the space I want whilst still keeping it having the same function.
*Edit, I didn't mean to have this as a duplicate, I didn't know that a "Delimiter" even was let alone that it was part of my problem. Apologies for wasting your time.
Change this
var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(" ")[1]));
to this
var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(' ')[1]));
Related
This question already has answers here:
int array to string
(13 answers)
Closed 2 years ago.
Hi having array of ints like ages int[]{123, 234}
how to select it to string which can be used as get request ages=123&ages=234
You can use a combination of string.Join and Select:
string.Join("&", ages.Select(age => $"ages={age}"));
To do proper URL encoding, you can use HttpUtility.ParseQueryString() like this:
var query = System.Web.HttpUtility.ParseQueryString(string.Empty);
foreach (var age in ages)
{
query.Add("ages", age.ToString());
}
// or: ages.ToList().ForEach(age => query.Add("ages", age.ToString()));
return query.ToString();
However, this will lead to ages=123,234.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I have a list of files (some ends with _HHMMss.* where * is the extension).
I want to check if a specific file exist in the list ignoring the "_HHMMss", means:
if I have a list:
A_Log_YYYY_MM_DD_121122.csv
B_Log_YYYY_MM_DD_112211.csv
C_Log_YYYY_MM_DD_333333.csv
D_Log_YYYY_MM_DD_555555.csv
E_Log_YYYY_MM_DD_777765.csv
check if "A_Log_YYYY_MM_DD.csv" exist, the answer in this case is TRUE
Is there a fast way to do that?
var list = new List<string> { .... }; // add your strings here
Regex reg = new Regex("[A-Z]_Log_[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{6}.csv");
bool matches = list.Any(x => reg.IsMatch(x));
The linq query (.Any(...)) stops when the first matching element is found.
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:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have a simple code where I receive data from database:
foreach (DataRow row in tmpDatosModulos.Rows)
{
tmpBSCID += row["ModuloURL"].ToString();
tmpBSCID.Replace("../BSC/wf_BSC_Reporte.aspx?BSCID=", "");
}
Convert.ToInt32(tmpBSCID);
First tmpBSCID receive value like: ../BSC/wf_BSC_Reporte.aspx?BSCID=21 now I want to replace it to drop all this part: ../BSC/wf_BSC_Reporte.aspx?BSCID= and get only last digits after =, but when I debug and it pass Replace instrucion it return all value: ../BSC/wf_BSC_Reporte.aspx?BSCID=21 instead of 21. Why it occurs? Regards
tmpBSCID = tmpBSCID.Replace("../BSC/wf_BSC_Reporte.aspx?BSCID=", "");
Methods on .NET strings do not change the string, they return a new string.
This question already has answers here:
Closed 14 years ago.
Closed as exact duplicate of this question.
I have an array/list of elements. I want to convert it to a string, separated by a custom delimitator. For example:
[1,2,3,4,5] => "1,2,3,4,5"
What's the shortest/esiest way to do this in c#?
I have always done this by cycling the list and checking if the current element is not the last one before adding the separator.
for(int i=0; i<arr.Length; ++i)
{
str += arr[i].ToString();
if(i<arr.Length)
str += ",";
}
Is there a LINQ function that can help me write less code?
String.Join(",", arr.Select(p=>p.ToString()).ToArray())
String.Join(",", array.Select(o => o.ToString()).ToArray());