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.
Related
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:
Most efficient way to concatenate strings?
(18 answers)
Make String concatenation faster in C# [duplicate]
(6 answers)
Closed 5 years ago.
What I had tried till Now
string Value ="";
foreach (List<string> val in L1)
{
Value = Value + string.Join(",", val) + " // ";
}
Where L1 is of datatype List <List<strings>>
This Works, But its take almost n half hour to complete
Is there as many fastest and simple way to achieve this.
I'd suggest use StringBuilder instead of concatenations in a loop like that:
StringBuilder builder = new StringBuilder();
foreach (List<string> val in L1)
{
builder.Append(string.Join(",", val) + " // ");
}
string result = builder.ToString();
When concatenating in a loop it needs to copy the string everytime to a new position in memory with the extra allocated memory. StringBuilder prevents that.
You can also refer to:
How to use StringBuilder wisely
How does StringBuilder work?
How the StringBuilder class is implemented? Does it internally create new string objects each time we append?
This question already has answers here:
What is the .NET equivalent of PHP var_dump?
(5 answers)
Closed 6 years ago.
I need to dump the content of arrays or objects and I am interested to know if in C# we have something like PHP instruction var_dump.
The objective is to not build a loop to use every property or content of array or object and print with Console.WriteLine.
The closest thing would probably be string.Join:
Console.WriteLine(string.Join(", ", myEnumOfObjects));
It would not automatically include "every property or content of array or object" into the output, though - if you want that to happen, you need to override the ToString method of the object being printed:
class MyObject {
public string Name {get;set;}
public DateTime Dob {get;set;}
public override string ToString() {
return string.Format("{0} - {1}", Name, Dob);
}
}
I think there aren't direct equivalent of var_dump php function.
You must use reflection to write an equivalent function.
If you search in web, you can easily find code which do it.
For example : http://ruuddottech.blogspot.fr/2009/07/php-vardump-method-for-c.html
When you insert a break point you can easily view the contents of an array by hovering your mouse over it.
or any of these:
You are probably using Console.WriteLine for printing the array.
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.WriteLine(item.ToString());
}
If you don't want to have every item on a separate line use Console.Write:
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.Write(item.ToString());
}
or string.Join (in .NET Framework 4 or later):
int[] array = new int[] { 1, 2, 3 };
Console.WriteLine(string.Join(",", array));
from this question: How to print contents of array horizontally?
I know you want to avoid loop, but if its just for the sake of writing multiple lines of code, below is a one liner loop that could allow you to print data with single line for Objects extend ForEach Method
List<string> strings=new List<string>{"a","b","c"};//declare one
strings.ForEach(x => Console.WriteLine(x));//single line loop...for printing and is easier to write
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);
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); } ));