Find what number a string is in a List [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to be able to know what number a string is in one list so I can match it with a string in another list.
Something like:
myList[1] would be 1
myList2[5] would be 5
Sorry if I'm not clear, but this is really hard to explain.

The number you're referring to is called the index.
You can find the index of a string in your list by using the List<T>.IndexOf() method.
int indexOfFoo = myList.IndexOf("foo");

To find the index of a string in a list of strings:
var myList = new List<string> { "x", "y", "z" };
var indexOfY = myList.IndexOf("y");

You can use myList.IndexOf("string") to get the index of the item in your list.
http://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx
I'm not sure if that is what you're asking, but that is how I understood it.

try it:
yourList.IndexOf("urstring")

Related

How do I add the sum of the columns of a 2d List<> in a 1d List<> in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I basically want to go through the 2d List<> and deposit the sum of the items in a column to a 1d List. So far have a double for loop but I cant figure out the logic. Any help with how I can start to contruct it logically would be helpful. Thank you. This is the code I have so far:
So if you have this:
var grid = new List<List<int>> {
new() { 1,2 },
new() { 3,4 }
}
You can do:
grid.Pivot().Select(c => c.Sum());
And you'll get an enumerable that is { 4, 6 } (1+3 and 2+4)
You can get Pivot from fubo's answer here (or choose any one of the other suggested transforms)
If I misunderstood and you wanted {3,5} just skip the Pivot part

C# Multidimensional string Lists [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For my knowledge I want to understand about multidimensional string Lists in C#. Please can some one explain me with good example code of creating multidimensional string Lists and also retrieving data from it.
I want examples for the following
List<string[]>
List<string[,]>
List<List<string>>
Declare them just like any other list type:
List<string[]> list1 = new List<string[]>();
List<string[,]> list2 = new List<string[,]>();
List<List<string>> list3 = new List<List<string>>();
and access them like so:
// First and third use identical indexers:
string s1 = list1[0][0];
string s3 = list3[0][0];
// Second is similar, just with a multidimensional indexer instead
string s2 = list2[0][0,0];

String contains a lot of copies, how to get rid of them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

How to display HashSet<SortedSet<string>> collection in richTextBox? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have HashSet> collection. I want to display content of this HashSet in richTextBox ? How to convert this HashSet to right type ?
Try :
richTextBox.Text = string.Join("", yourSet.ToArray());
try this instead:
foreach (SortedSet<string> set in youHashSet)
{
richTextBox.Text = string.Join("", set.ToArray());
}
Well a little bit of more information would have helped. It's hard to say without seeing your code. But these are your basic steps
after you build your Hashset
then for a for or foreach loop on it
and then ToString the object if it's not a Hashset
string helloWorld = string.empty;
foreach(<whateverobject> myobject in Hashset<whateverobject> myHashsetList)
{
helloWorld = helloWorld = myobject.ToString();
}
richTextBox.Text = helloWorld;
If you delimit it or whatever that's up to you

Reorder a List of string using LINQ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a list of string List which contains all state names.
I need to move few states(Ex New York, California etc) to appear at the top of the list.
How can I do this using LINQ?
FYI: the list is alreday sorted in alphabetical order.
Its just a simple List and few important states needs to be at the top. No criteria.
The following is a proof of concept using letters instead of states.
var all = new List<string>() {"a","b","c","d","e"};
var top = new List<string>() {"c","d"};
var finalList = top.Concat(all.Except(top));
The idea is that you have your full list of states (all) and a list of the ones you want at the top ('top'). You then take the ones at the top and concatenate the list of the remaining ones that you create using the Except method.
List<string> importantStates = {...};//It's up to you
var list = states.OrderBy(x=>!importantStates.Contains(x));

Categories