This question already has answers here:
How do I clone a range of array elements to a new array?
(26 answers)
Closed 7 years ago.
How, using C#, can I create a new string[] from another string[] containing a subsection of the old string[] array?
For example, if I had:
string[] a = { "cat", "dog", "hamster", "parrot" }
And I wanted to get everything after the first element I should get:
string[] b = { "dog", "hamster", "parrot" }
How, if this is even possible without a for loop, do I achieve this?
You can use some extensions methods to solve it, that is the case for Skip(int) under the System.Linq namespace, for sample:
string[] b = a.Skip(1).ToArray();
And it will skip 1 element and convert the result into a new array. Remember to add the namespace:
using System.Linq;
As the MSDN documentation said:
Bypasses a specified number of elements in a sequence and then returns
the remaining elements.
Related
This question already has answers here:
Method to check array list containing specific string
(4 answers)
Closed 3 years ago.
I got an ArrayList in C#
ArrayList myAL = new ArrayList();
now I add a string to it
myAL.Add("Hello");
How can I now figure out if the String "Hello" exist in the array. I know it's possible to loop over it, but I think there must exist a function or something.
You can simply use the method Contains from System.Collections
Microsoft Doc - Contains
For your example :
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
if (myAL.Contains("Hello"))
{
// Do something
}
This question already has answers here:
Sort String array in custom order
(4 answers)
Fastest way to get list in custom order
(2 answers)
Closed 3 years ago.
How can I sort an array using a predefined sorted array?
I'm working with a web API that you can query for a list of information, which you can specify the things you need in the list. The data list gets returned separated by newlines.
The problem is that the API returns the information in a specific order, regardless of what order you specify yourself.
For example,
Query("second,third,first,fourth");
// returns string:
#"Info for first
Info for second
Info for third
Info for fourth"
I then have to parse it into a dictionary:
{ "first", "Info first" }, {"second", "Info second"}, etc
I could just base it off the parameter list I used, however unless you memorize the correct order for all data, it's a bit annoying.
So, how could I sort it using a predefined sorted list. Such as:
// All possible queries sorted correctly
{ "first", "second", "third", "fourth", "fifth", etc }
// My unsorted list
{ "third", "first", "fifth"}
// Would become:
{ "first", "third", "fifth"}
(These are placeholder values to make it more clear)
You can sort them using a method to compare these results to determine which one comes earlier according to your list
private bool IsBefore(string A, string B)
{
int iA, iB;
iA = Array.IndexOf(RefArray, A);
iB = Array.IndexOf(RefArray, B);
if (A < B)
return true;
return false;
}
This question already has answers here:
How do I make my string compare not sensitive to (ignore) minor differences in white space?
(4 answers)
Closed 3 years ago.
I've tried several methods to remove duplicate elements from an array of strings, but none of them do what I want. Here are 2 strings:
CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//
CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//
I want just one of these to be retained as they are copied from array a to array b. It doesn't matter which one.
I have tried IEnumerable, HashSet, and Distinct. Each of them returns both strings. (An error of mine duplicated the second string. Sorry. To be clear, I want the compare to ignore whitespace.)
IEnumerable<string> b = a.AsQueryable().Distinct(StringComparer.InvariantCulture);
HashSet<string> set = new HashSet<string>(a);
string[] b = new string[set.Count];
set.CopyTo(b);
string[] b = a.Distinct().ToArray();
The first element isnt the same as the others, so distinct will not gonna work for this, you must replace the space char.
string[] a = { "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//", "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//", "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//" };
string[] b = a.Select(p => p.Replace(" ", "")).Distinct().ToArray(); //Replace
output:
"CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//",
This question already has answers here:
Initializing collections that contain nested collections
(3 answers)
Closed 6 years ago.
I have to make two lists in C # . How do the second list ?
The first is well and works , the second already tried as exemplified below but does not work . And I want it to be exactly like or similar without the use of other variables
List<String> t = new List<string>() {"question","anotherQuestion"};
then i want another list
and i would like to initialize it like this.
List<String[]> t = new List<string[]>()
{
("rightawnser","wrongAwnser"), //1st question
("rightawnser","wrongAwnser"), //2nd question
};
Close, but you have to create an array for each list entry:
List<string[]> t = new List<string[]>()
{
new[] { "rightawnser","wrongAwnser" }, //1st question
new[] { "rightawnser","wrongAwnser" }, //2nd question
};
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
How can i add an item to a member of List<> array?
please see the example below :
List<string>[] array_of_lists = new List<string>[10];
array_of_lists[1].Add("some text here");
but a have the error below :
Object reference not set to an instance of an object.
what this error mean and how can i fix it?
You have initialized the array, but all elements are null yet. If you want to initialize it with a List<String> at a given index you can't use Add which is a method of List<T>.
In this way you initalize the array at the second element:
array_of_lists[1] = new List<string>{"some text here"};
Note also that indices start with 0, not 1.
Here's a demonstration.
After so many edits, changes and commented answers I´d like to give a complete solution for you:
List<string>[] array_of_lists = new List<string>[10];
for (int i = 0; i < array_of_lists.Length; i++) {
array_of_lists[i] = new List<string>();
array_of_lists[i].Add("some text here");
array_of_lists[i].Add("some other text here");
array_of_lists[i].Add("and so on");
}
problem is that when you initialize an array, it is created with default values of items. For most of the value types (int, float, vs... ) default value will be 0. for reference types (strings and nullable and List and many other) default value will be null.
so your code should be like this
List<string>[] list_lines_link_scanner_ar = new List<string>[int.Parse(txt_ParaCount_In_LinkScanner.Text)];
// this is the line -->
list_lines_link_scanner_ar[1] = new new List<string>();
// <----
list_lines_link_scanner_ar[1].Add("some text here");
I think you mixed List<T> and arrays.
From MSDN
The List<T> class is the generic equivalent of the ArrayList class. It
implements the IList<T> generic interface using an array whose size is
dynamically increased as required.
So, easyly you can write,
List<string> array_of_lists = new List<string>();
array_of_lists.Add("some text here");
Declare:
List<List<string>> listOfList = new List<List<string>>();
Add:
listOfList.Add(new List<string> { "s1", "s2", "s3" });
unless you really need an array.