VB .Net concatenation/loop
I have this line of code which I want to conca开发者_如何转开发tenate -or at least solve the loop problem...
test = 1 - ("0." & thisnumber(0) & thisnumber(1) & thisnumber(2) & thisnumber(3) ... thisnumber(500) )
I want this to have a loop in it...
I simply want to get all the array values into 1 variable sort of thing, -as it is too long for a decimal. -So I want it to loop and work test out.
-Increasing thisnumber() (-Which is an array holding values e.g. 2,5,0,0,0,0,0,0,3,0,0,1)
Until it gets to about 500,
Can some implement a loop into this?
Or suggest a way.
forgive the untested C# syntax:
var intArray = new StringBuilder();
intArray.Append("0.")
foreach(var number in thisnumber)
{
intArray.Append(number.toString());
}
var test = 1- Double.Parse(intArray.toString());
You could do that using LINQ (though I'm not clear what the 1 - (...
portion is supposed to be, since you cannot subtract a string from a number).
To deal with the relevant portion of your question--"looping" the array access portion:
Dim data As String = String.Join((From Idx in Enumerable.Range(0, 500) Select thisnumber(Idx).ToString()).ToArray(), "")
That will concatenate all of the elements of the array into a single string.
This addresses the literal requirement of your question (looping only the array access part), but I would recommend you go with another, more readable solution, such as the StrinBuilder
-based approach that another user has already outlined.
string.Concat will take an array of strings...
double d = 0.0;
string[] values = new[] { "1", "4", "0", "9" };
d = 1 - double.Parse(string.Concat("0.", string.Concat(values)));
Use a generic List(of Int32) for this because its much more readable and performant. Add your numbers to the list, then iterate through it and append the numbers to a StringBuilder.
精彩评论