How to number values in string array with int [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 3 years ago.
Improve this question
I have some strings in string array.
Now I wanna to give before first string prefix 1: string..etc
Example:
Test
Test
Test
Expected result:
1. Test
2. Test
3. Test
Is that even possible? Thanks!

You could use Enumerable.Select (Linq) with Index, and use string formatting to include Index as prefix of string.
For Example
var result = strCollection.Select((x,index)=> $"{index+1}.{x}");

If you want to do this in-place with an array of strings (without creating a new array), you can't use Linq. Instead, just use something like this:
public static void AddNumberingPrefix(string[] strings)
{
for (int i = 0; i < strings.Length; i++)
{
strings[i] = $"{i+1}. {strings[i]}";
}
}
Example usage:
string[] items = {"First", "Second", "Third"};
AddNumberingPrefix(items);
Console.WriteLine(string.Join("\n", items));
This outputs:
First
Second
Third

There is an overload of LINQ Select that gives you the index:
yourList.Select((el, idx) => $"{idx+1}. {el}");

Related

How to sort by name an Enum 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 4 months ago.
Improve this question
I was trying to sort an Enum by the names, I have the following code:
Enum.GetNames(x.options).Where(.....).Select(.....)
notice that x.options is an enum
and I would like to sort de results of the linq by the name of the Enum, how could I do it?
Assuming the following enum:
public enum Number
{
One = 1,
Two = 2,
Three = 3,
Four = 4,
}
You need to pass the enum's type in Enum.GetNames and then call OrderBy (to sort in ascending order):
var sortedEnum = Enum.GetNames(typeof(Number)).OrderBy(x => x);
// or, from .NET 5 onwards:
var sortedEnum = Enum.GetNames<Number>().OrderBy(x => x);
This will return an IOrderedEnumerable<string> having the following (sorted) values:
Four
One
Three
Two
Of course you could .ToList() or .ToArray() the results, to return them in a list or an array, respectively.

Parameters in parentheses after a method name: what are they and what do they do? [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 3 years ago.
Improve this question
Newb to C# and OOP. My journey thus far has been to take code bases that I've inherited from former developers and either address issues, or add enhancements, whilst trying to understand said code bases' structures from front-to-back.
I'm having trouble fully grasping the concept around the parameters which follow the initial declaration of a method. Here's an example of a method I'm working with:
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity entityManagement = new Entity();
string[] lineParts = line.Split('|');
entityManagement.Identifier = lineParts[0];
entityManagement.ProductId = 1234;
entityManagement.Category = "ABCDE";
entities.Add(entityManagement);
}
return entities;
}
The part after ParseCsvFile in parentheses: (List<string> entries, string urlFile)
Could someone explain what these are and what they do, perhaps with metaphors/analogies/real-world examples?
It might be easier to see their purpose if you look at a simpler function for example:
public int Add(int number1, int number2)
{
return number1 + number 2;
}
Above there is a function that adds two numbers together and returns the result. It is a set of instructions to follow. How can it follow the instructions if it doesn't know what numbers to use.
That's where calling the function comes in.
for example:
var result = Add(2, 5);
In this scenario result = 7.
2 is replacing number1 in the function and 5 is replacing number2.

How can I make each array, within an array perform a task? [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 6 years ago.
Improve this question
I am getting data with one of the following names, Adam, Bob, Cam, Dan, Earl, or Fred.
I only want certain pairs to operate on each other. Right now I have the string:
string list="Adam-Bob;Cam-Dan;Earl-Fred";
Then I split them via the semicolon
string[] splitList=list.Split(';');
Now I have an array of pairs as so
Adam-Bob Cam-Dan Earl-Fred
[0] [1] [2]
Ideally, I would like to perform an operation on each of them, but instead I find that I can only do the following:
Split via ','
foreach (string s in splitList)
{
string firstPerson=splitList[0];
string secondPerson=splitLilst[1];
if (UDPoutputData.Contains(firstPerson)==true)
{
//record data into string for firstPerson
}
if (UDPoutputData.Contains(seoncdPerson)==true)
{
//record data into string for secondPerson
}
//if I have data for firstPerson AND secondPerson, perform operation and give me the output
}
Unfortunately, if I get the name Adam, followed by Cam, my operations are disorganized. Perhaps I need to automatically create a string for each name? Or is there an eloquent way of operating the data on the first array...
You could get an array of arrays (of string), like this:
string[][] splitList = list.Split(';').Select(pair => pair.Split('-')).ToArray();
Then you can access splitList[0][0] to get Adam, splitList[0][1] would be Bob, splitList[1][0] would be Cam, etc.
So your loop becomes:
foreach (string[] pair in splitList)
{
string firstPerson=pair[0];
string secondPerson=pair[1];
// ...

How to pass array to method [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
It's probably a stupid question, but anyway.
My problem is that I can't pass uninitialized array, but I don't know if my array need to hold 5 or 30000 elements, eg. so it will be a waist of memory to initialize big array.
Should I use List<T> instead, or?
I've noticed people tend to return array instead of list, which is mutable, and therefore much more convenient, so there must be a performance issue with lists. Is that so?
make it into an 'out' parameter and all should be well:
private void x()
{
string sTestFile = "this is a test";
string[] TestFileWords;
FixConcatString(sTestFile, out TestFileWords);
}
private void FixConcatString(string splayfile, **out** string[] sWordArray)
{
char[] charSeparators = new char[] { '-' };
splayfile = splayfile.ToLower();
splayfile = splayfile.Replace(#"\", " ");
sWordArray = splayfile.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
}

Getting individual string length in an array [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 8 years ago.
Improve this question
I am trying to write a program which does the following:
There is a method called lengthOffTheWords. It receives an array of strings, and returns an array of numbers which represent the length of each individual string.
Ex: For the following input
{"I", "know", "a" , "friend"} the method returns {1,4,1,6} .
{"yes"} the method returns {3}.
{"me", "too"} the method returns {2,3}.
I would like to see an example of how to write it.
I would do something like this:
public int[] LengthOffTheWords(string[] array)
{
return array.Select(item => item.Length).ToArray();
}
I did not test this but it should do what you want.
int[] GetLengths(string[] array)
{
int[] structure = new int[array.Length];
for(int i = 0;i < array.Length;i++)
{
int length = array[i].Length;
structure[i] = length;
}
return structure;
}

Categories