This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there an easy way to return a string repeated X number of times?
In Python you can multiply sequences like this
fivespaces= ' ' * 5
Is there any built-in equivalent for this in C#? (without operator overloads or class extensions)
If it's just a string then you can return multiples by passing in a count to string()
var fivespaces = new string(" ", 5);
In the case where you want a collection of something else like a custom type, you can use Enumerable.Repeat to get a collection:
var items = Enumerable.Repeat(new SomeModel(), 5);
Related
This question already has answers here:
What is the fastest way of converting an array of floats to string? [duplicate]
(5 answers)
Closed 3 months ago.
i am trying to connvert
double[] v = { 5, 4, -8, 2, 6 };
to a String with a method.
I created an method called ToString(double[] v);
and tried to do it with an foreach loop, but every time i insert a double the console gives out system.double instead of the string.
for university i am only allowed to use convert.toString and no parse and i should use it as a method
Thanks for your support.
Benjamin
The correct way to do this would be to use string.Join
var myString = string.Join(", ", myDoubles);
But if you want to do the same thing yourself it is fairly easy to do:
var sb = new StringBuilder();
foreach(var myDouble in myDoubles){
sb.Append(Convert.ToString(myDouble)).Append(",");
}
var myString = sb.ToString();
If you are not allowed to use stringBuilder either you can just concatenate strings instead, just keep in mind that increases the algorithmic complexity, and is not really something you should do, or even teach as an example.
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:
count objects of a certain type in a collection and use this as a string in a textbox
(1 answer)
Finding count of particular items from a list
(5 answers)
Get Count in List of instances contained in a string
(6 answers)
Get item count of a list<> using Linq
(3 answers)
count objects that meet certain condition in List-collection
(2 answers)
Closed 5 years ago.
I have a ReadOnlyCollection of a Smartcard type from Microsoft.Clm.Shared.Smartcards namespace.
one of the fields/parameters of the smartcard object is AssingnedUserName.
I need to be able to count how many times a smartcard with the same username exist in the list,
something like:
[Pseudo Code]
int count = (smartcardCollection.AssignedUserName == my String).Count().
I tried to use the ReadOnlyCollection.Tolist() method, but I couldn't find the correct syntax to make it work.
I also found many examples but non for a ReadOnlyCollection object !
what is the best practice for achieving this ?
thanks
David.
just use this
int count = smartcardCollection.Count(s=>s.AssignedUserName == my String);
LINQ Count it takes a function to test each element for a condition
You just need to use the overload of Count or Where ... Count:
int count = smartcardCollection.Count(s => s.AssignedUserName == my String);
or
int count = smartcardCollection.Where(s => s.AssignedUserName == my String).Count();
This question already has answers here:
C# List of objects, how do I get the sum of a property
(4 answers)
Closed 5 years ago.
I have the following: List<OutputRow> which contains a number of OutputRow objects.
I am wondering if there is a way for me to use a lambda function on the list to return the total sum of the values of a certain propertyX on each OutputRow object in the list.
Example list:
OutputRow.propertyX = 4
OutputRow.propertyX = 6
OutputRow.propertyX = 5
return 15
Test data
var ls=new List<OutputRow>();
ls.Add(new OutputRow(){propertyX=4});
ls.Add(new OutputRow(){propertyX=6});
ls.Add(new OutputRow(){propertyX=5});
Lambda
var total= ls.Sum(x=>x.propertyX);
SOmething like this:
var yourSum = yourOutputRowList.Sum(x => x.propertyX);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Split string, convert ToList<int>() in one lineā¦
i have a string that looks like this.
string s = "1,6,4,3,5,7,4";
and i want to convert this into an array of integers.
what is the best and fastest way of doing this in C#?
use split method.
int[] array = s.Split(',').Select(str => int.Parse(str)).ToArray();
Hmm, don't know if it is fastest way, however it is the simplest way :)
Hope this helps :)
int[] i = Array.ConvertAll(s.Split(','), new Converter<string, int>(delegate (string str) { return int.Parse(str); } ));