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.
Related
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]));
This question already has answers here:
Convert a list to a string in C#
(14 answers)
Closed 4 years ago.
I wanted to ask if it's possible to change List items to one string?
List<string> example = new List<string>();
example.Add("1");
example.Add("2");
example.Add("3");
string text = "123";
string.Join is for you
string s = string.Join("", example )
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:
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 );
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();