Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
you can see that value is 74 but its not going inside the if why?
Because you have a space in TEMP. " 74". The two values are not equal because the second one TEMP2 is "74" without the space.
Try using Trim on your values to remove whitespace:
string temp = ids[i].Trim();
string temp2 = raditem.Value.ToString().Trim();
Also, always use the .Equals overload for the String class, as you can control the culture for the string comparison. In this case it doesn't matter because they're numerals but it could matter if you want to compare to alphabetical strings where you want "A" to equal "a" and so forth.
Furthermore, if you are sure your array contains only numbers, I could recommend converting the values to numbers before comparing them.
// one way to validate is wrap this in a try-catch and handle input format error.
int temp = Convert.ToInt32(ids[i].Trim());
That way if you have a case where there aren't numbers, you could validate and complain to the user or whatever you want.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 24 days ago.
Improve this question
I am a new user to C# and I am having difficulty with one last thing to finish this script I am working on. I am in .NET 5.0. I am working on writing some results out to a csv file but when I use the string.Format() method, I am only getting the first value returned to the console, or the csv for that matter.
string newLine = string.Format("Value1", "Value2");
Console.WriteLine(newLine);
I am inside of a loop, and all lines are being returned, buy only Value1 is being returned on each line, not Value2. Any help would be appreciated.
I don't think you understand how string.Format() works at all. The first argument is a base string, which should include placeholders (ie: {0}, {1}, etc). Additional arguments then fill in the placeholders in order.
So if you want CSV data, it sounds like you want something more like this:
string newLine = string.Format("{0},{1}", "Value1", "Value2");
Console.WriteLine(newLine);
Or from an array like this:
string[] row = {"Value1", "Value2"};
string newLine = string.Format("{0},{1}", row);
Console.WriteLine(newLine);
Or maybe you'd prefer string.Join(), which can handle an arbitrary number of values without needing placeholders:
string[] row = {"Value1", "Value2"};
string newLine = string.Join(",", row);
Console.WriteLine(newLine);
But really, even simple CSV files have a surprising number of edge cases and gotchas, and you'll likely be MUCH better off to get a CSV library from NuGet.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The testCards string picks up the value from web.config. But every time I try to use int.tryParse it gives a false value when I try to parse the string testCards. Any idea what I might be missing?
<add key ="TestCards" value ="4987654321098769,4111111111111111,4987654321098769"/>
string testCards = ConfigurationManager.AppSettings["TestCards"];
int flag=0
bool isSuceeded=false
isSuceeded = int.TryParse(testCards, out flag);
It's not an int! It has commas in it, and if it didn't have commas the number is much bigger than an int can hold in any .NET platform.
I'm gathering from the name TestCards that these are intended to be credit card numbers? In that case they should be strings.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
An public string ItemSubType which is assigned a string "primary" by another script is somehow not equal to the string "primary". I've checked for empty characters but there isn't any. I've been absolutly stumped by this but also interested in how this is happening.
Debug.Log("---primary---");
Debug.Log("---" + ItemSubType + "---");
if(ItemSubType != "primary")
{
Debug.Log("This is ridiculous!");
}
Here is logs:A picture of the log
Thank you all for the comments!
After browsing through some other questions, I found .Trim().
Mystically there seems to be an invisible character in the string that isn't a whitespace character, as would be evident by the lack of spaces when adding '---' to either side.
By doing this:
ItemSubType = ItemSubType.Trim();
It fixed the Issue. To give some context, I pulled the string data from a csv file. I checked the file for any extra spaces but there wasn't any. Not sure if this is related tho.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
When posting data from view, I send it to the Controller via ajax and split the date time value, then the parameter becomes a string and I want to convert it to my datetime, but I encounter such an error
There is a blank/space at the end of your 'date' string. Use bgdate.Trim(). You may also check that bgdate is a non null string and have then a default value with something like (bgdate == null)?"01-01-1970":bgdate.Trim().
There's a white space in your value 08-04-2021 . You should Trim() it:
bgdate.Trim()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am using IndexOf() and it does not work.
The code is:
if (sqlex.Message.IndexOf("Critical") > 0)
and Message does contain the word - Critical (see image), but does not equate to true. It takes the false path.
I also put in test code:
int index = sqlex.Message.IndexOf("Critical");
and index is = 0.
Why?
The first position in the string is 0, not 1. So if your string is
Critical Error - do not continue. Contact IT...
And you search for Critical, then 0 is the expected result.
If the string is not found, the result will be -1 ... unless the string is also empty, so make sure to check that, too.
The method string.IndexOf returns -1 if the character or string is not found. 0 means it is found. Change your code to if (sqlex.Message.IndexOf("Critical") >= 0)