This question already has answers here:
How to convert string to double with proper cultureinfo
(7 answers)
Closed 3 years ago.
I'm reading .txt file and spliting values into array like this:
for (int i = 0; i < articleItems.Length; i++)
{
List<string> splitStrings = articleItems[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList();
splitStrings[0] = splitStrings[0].Substring(12);
}
After that I'm trying to make object from those values, and everything is fine, but strange thing are happening here...
Here is the image from debugger, there is 14.80 at the beginning and when I convert that string to decimal it becomes 1480...:
Try
Double.Parse(splitStrings[2].ToString())
Related
This question already has answers here:
Read numbers from the console given in a single line, separated by a space
(7 answers)
Split string, convert ToList<int>() in one line
(11 answers)
Closed 3 years ago.
I have gone through this solution. But this is not solving my problem. Let's say I have a string:
var aString = "0 -1 12 456 -512";
I want to convert this string to an int array like:
var convertedArray = [0, -1, 12, 456, -512];
How should I approach to solve this problem?
You can simply do this:
var stringNumbers = aString.Split(' ');
var numbers = new int[stringNumbers.Length];
for (int i = 0; i < stringNumbers.Length; i++)
numbers[i] = Convert.ToInt32(stringNumbers[i]);
var convertedArray = Array.ConvertAll(aString.Split(' '), int.Parse);
This question already has answers here:
Does C# support a variable number of arguments, and how?
(4 answers)
Closed 5 years ago.
I'm trying to pass a variable number of arguments here.
Codification codebook = new codification(data,"Attr1","Attr2",..."AttrnumOfAttrColumns,"Result")
and also here:
int[][] inputs = symbols.ToJagged<int>("Attr1","Arrt2",..."AttrnumOfAttrColumns);
I was trying to do it with a for loop which is not the right.Is there a way to do this?
Codification codebook = new Codification(data, for (int i = 0; i < numOfAttrColumns; i++) {return "Attr"+Convert.ToString(i) }, "Result");
According to Accord.net manual
You can put
Codification codebook = new codification(data, new string[] {
"Attr1", "Attr2", ..., "AttrnumOfAttrColumns", "Result"});
This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);
This question already has answers here:
Array Size (Length) in C#
(9 answers)
Closed 6 years ago.
how should I convert Java loop code to C#?
Java:
for (int i = 0; i < edges[index].length; i++) {
edges[index][i] = 0;
edges[i][index] = 0;
}
I'm stuck with the edges[index].length part, is there any similar method in C#?
For reference, edges is int[,] array, index is some integer.
Try using GetLength?
edges.GetLength(index);
http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx
In C#, Properties and Methods should start with an uppercased letter. Try Length instead of length
This question already has answers here:
Identify if a string is a number
(26 answers)
Regex for numbers only
(20 answers)
Closed 8 years ago.
How to check the string is number or not. I am verifying mobile number codes in which it should have 10 digits and only in numerical format.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
I am new to programming. please help me
Use int.TryParse:
int i;
if(str.Length==10 && int.TryParse(str, out i))
{
//Code goes here
}
Another way which has issues with unicode digits is using Char.IsDigit:
if(str.Length==10 && str.All(Char.IsDigit))
{
}