Find String Array in other String Array [duplicate] - c#

This question already has answers here:
Finding a subsequence in longer sequence
(8 answers)
Closed 1 year ago.
I want find a string array in other string array. How I do that?
Example:
string[] a = {"a","b","c","d","e","f","g","c","d","e"}
string[] b = {"d","e"}
How find b in a, I need get the index of all instances.

I would suggest you search a bit of the documentation to understand more of the language but anyway I'll try to explain what you can do
You can do a for loop going through the a array and inside you do a for loop going through the b array, if a[index] = b[index2] you break out of the a loop and put the index in a new array that you initialized before
If you need more help than that say but I recommend going through more documentation and a few videos and learn a bit more the basics

Related

I saw this in code to do with lists and I'm not sure what it does [duplicate]

This question already has answers here:
What does it mean when there is a comma inside an array declaration? e.g. float[,]
(4 answers)
Closed 2 years ago.
So I'm trying to learn from other peoples code and I saw this at the end of a list and could not find it anywhere I looked
List<IMyShipMergeBlock>[,] merges = new List<IMyShipMergeBlock>[3, 2];
What is [,] and [3,2] called and could you point me to where I could learn more about it thanks
its a multidimensional array of List<IMyShipMergeBlocks>
The [3,2] is the initialiser for the array
so imagine a 3x2 grid and in each square there is a space for a List<IMyShipMergeBlocks>
EDIT: And important to note: You would then need to initialise each list separately as with the above you have an array of nulls to start with

Can't get 2 different Substrings from a String C# [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 4 years ago.
I have a task.
I should extract two substrings from a string.
The lenght of the String will be different each time, so the method should be generic.
Suppose I have the following String:
/*Description:\r\n*RANGE:\r\n*HIGH\r\n*LOW\r\n*/
I need to get the substring1= HIGH and substring2= LOW.
The substring1 and substring2 will be all the time between \r\n, but they values will be different.
I would be very grateful if anybody helps me. It can be a pseudocode, anything.
Thanks in advance.
UPDATE1: I'm searching first for "RANGE:\r\n*" and get the index of the character * and the index og character "H". But next don't know how to get the whole substring.
If the pattern you've provided is similar to what you'd expect all the time, a stupid simple approach would be:
public static string[] GetParts(string input)
{
string[] parts= input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
return parts.TakeLast(2).Select(item=>item.Replace("*", string.Empty)).ToArray();
}
Note: This is not a production quality code.

Efficient way of counting every occurrences of every words from a URL [duplicate]

This question already has answers here:
Counting the occurrences of every duplicate words in a string using dictionary in c# [closed]
(3 answers)
Closed 6 years ago.
I am making something like, the user will input any url and the text will be obtained.
The text will then be parsed and the words will be counted.
I am currently reading this article from microsoft:
https://msdn.microsoft.com/en-us/library/bb546166.aspx
I can now get the text and i am currently trying to think of an efficient way to count every words.
The article example required a search data but i need to search every word and not a specific word.
Here is what i am thinking:
get the text and convert it to string
split them (delimiters) and store in array
loop through the array then check every occurrences of it.
would this be efficient?
Using Linq
If you have a small amount of data can just do a split on spaces, and create a group
var theString = MethodToGetStringFromUrl(urlString);
var wordCount = theString
.Split(' ')
.GroupBy(a=>a)
.Select(a=>new { word = a.Key , Count = a.Count() });
see fiddle for more a working copy
Some Experiments and Results
Messed around in .net fiddle a little bit and using Regexs actually decreased the performance and increased the amount of memory used see here to see what I am talking about
Other alternative
Because you are getting the request from a Url it might be more performant to search inside of the stream before converting it to a string and then performing the search
Don't optimize unless you need to
Why do you need to find a performant way to do this count? Have you run into any issues or just think you will, a good rule of thumb is generally not to prematurely optimize, for more information check out this good question on the topic : When is optimisation premature?

How do i determinate if a sentence has digits/numbers, and then assign those numbers to variables? [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 7 years ago.
I already tried to determinate the digits in a sentence using 'isDigit', but this gives me a 'bool' output. I need an 'int' output.
What i want to do is, say, i have the sentence "cheese23"; "2" and "3" will be put in their own variable, so i can add/subtract/multiply/ etc them.
(x=2,y=3;)
help will be hugely appreciated (self-teaching beginner here)
Do:
int[] intArray = "Cheese23".Where(Char.IsDigit).Select(c => int.Parse(c.ToString())).ToArray();
This extracts the numbers in the string in the order and creates an array out of it.
Then you can do intArray[0] to get 2 and intArray[1] to get 3.
Search up LINQ to see how those chain of methods did it.

How many string additions do you need for a StringBuilder to be more efficient? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Stringbuilder vs String.Concat
As I understand it, String are immutable, so if you do "one " + "two", rather than reusing the memory of these two strings, a new string is created with value "one two". This becomes a problem if you do something like:
String output = "";
for (int i = 0; i < 20; i++){
output = output + ii.toString();
}
Because 19 (or 39?) strings are created and discarded to produce the final desired output.
String builders are mutable, so you can add strings together efficiently, and then convert them into a string when you are done.
I assume there is some overhead in creating string builders, and also the clever compilers used today optimize away lots of string addition problems. So my question is: how many strings do I need to add together before it becomes more efficient to use a StringBuilder instead? Or are my assumptions incorrect?
An answer in this question - Does StringBuilder use more memory than String concatenation? - states 'a few dozen' is the threshold.
And linked from another answer in that question is this link - http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html - which concludes with "it doesn't matter"!

Categories