I have a dictionary name Pair in c#. It contains some key - value pairs. I added a screen shot, there was an exception generated on (p.Key == "left), Sequence containes no matching element but if you see in the Pair Dictionary, the left key is present there. Then please anybody tell me why this exception occured ?
If I can see it right, the left value is preceded with a whitespace, " left", so p.Key == "left" is never true.
Use p.Key.contains("left") instead, or check if it returns a value or not, before trying to manipulate it. Or trim the leading and trailing whitespaces.
If it isnt a white-soace issue, then the only way I can that happening is if the dictionary is a Dictionary<object,...>, in which case the == you are using is reference equality, not string equality. That would cause it to fail. If so, either use a Dictionary<string,...> or use Equals("left", p,Key).
Note that Pair["left"] would be a much better way of doing the lookup.
You should use indexer or TryGetValue instead of Single. With Single, there is no reason to use dictionary.
Also, your problem stems from the fact, that there is space before the 'left' so the whole string is actually " left". See how width is one character to the left.
Related
So i am facing a problem here which i am sure has a simple answer but i cannot seem to find it.
I am comparing string data from 2 tables using C# code
When the data is null or empty in both tables, i want the comparison to return "True" which basically means they are identical.
I am using string.IsNullorEmpty for checking null or empty conditions.
The problem is in one table, the string value is "" while the other table has the same value escaped and is appearing as "\"\""
I assumed using regex.unescape will solve this but it does not seem to be working and i am getting an output that both the values are different causing problems.
One solution i figured out is directly checking if str == "\"\"" for solving the problem.
But are there any cleaner options?
I think you are mixing things here.
If your strings come from the same data source, then either all of them are escaped, or they are not (and if that's not the case, you have bigger problems than what you are stating).
So, if they are not escaped, and one of them contains "", and the other one contains \"\", then they are not equal, one is 2 characters in length, and the other one is 4.
So I'm assuming that they are escaped and your first string is actually empty in the database (it doesn't contain any characters), and the second one is \"\".
You can then use Regex.Unescape (if they are always escaped), but those two strings are not the same: one is empty, and the other one contains (once unescaped), "", so the first string contains no characters and the second one has two of them: no wonder they won't be compared equal.
Now, iff they are indeed escaped, it does not make sense that one contains "", because those characters should be escaped. And if this is not the case, then you have a very specific problem which is not what you asked for: you need to determine whether your string comes escaped or not from the data source... and that's basically impossible unless there's a very specific set of rules which determine so.
If the data source contains randomly escaped or not strings, imagine your data source returns a string \"\": how do you determine if the actual content is escaped and it means {'"','"'} (2 characters, each of them being a double quote), or if it isn't, and it's 4 characters, representing {'\','"','\','"'} (one backslash, one double-quote, one backslash and one double-quote)? There's just no way to tell unless you have a specification that determines those rules (or another field saying if the string is escaped or not).
So, back to your question: although you haven't put any code, my guess is that it is just not wrong: either your expectatives are what are wrong (you want \"\" to mean a string is empty, but it doesn't, because it just doesn't mean that), or your data is wrong.
Either way, there's no generic code solution to any of those... there's specific code solutions for specific cases (like the one you are showing), but not a generic one: with the info you gave in your question, it's just impossible
After all this babbling, now for a specific answer, if your table A contains unescaped strings, and your table B contains escaped strings:
stringFromTableA == Regex.Unescape(stringFromTableB)
Should return true if stringFromTableA contains "" and stringFromTableB contains \"\". Check it. Neither of those will be empty, so string.IsNullOrEmpty() will return false
And an update: should you be checking those string values in the Visual Studio debugger, the debugger shows them escaped, so if you are seing "" in one and \"\" in the other, then your first string is empty (and string.IsNullOrEmpty will return true), and your second string contains two double quotes: string.IsNullOrEmpty will return false, since it is not actually null or empty. And Regex.Unescape will do nothing on this case, since your string doesn't contain any \ and there's nothing to escape, it's just the debugger showing those \'s.
I am using an if condition with "IsNullOrWhiteSpace(+textbox)" to refer to a text filed that has no value or whitespace only. However, now I need to know hot to specify a field that is not empty or whitespace only.
This is the code I wrote:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
But what if I want to specify to have the command run only when the textfield is NOT null or empty?
Thank you very much for any help. I am a beginner. Help is appreciated.
Have a great day!
string.IsNullOrWhiteSpace returns true if the text is null or whitespace. If the return value is false, then the text is populated with non-whitespace characters.
Search for where the condition is not true using !.
if (!string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
this is the equivalent of:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text) == false)
Both of the above will enter the if statement if pathofsrcfilesTOCOPY.Text is populated with non-whitespace text.
Worth noting, that although the accepted answer is correct, I find it easier to follow, when assigning its result to a variable and then evaluating that variable in the if condition.
bool isPathEmpty = string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text);
if (!isPathEmpty)
{
//...
}
I'm reading from a CSV file that has a lot of empty cells and I want to read in the empty cells as zero. Is there a way to do this using DelimitedReader?
I'm looking at the documentation for DelimitedReader and I see that it takes four parameters:
•sparse: Whether the the returned matrix should be constructed as sparse (true) or dense (false).
Default: false.
•delimiter: Number delimiter between numbers of the same line. Supports Regex groups.
Default: \s (white space).
•hasHeaders: Whether the first row contains column headers or not. If true, the first line will be skipped.
Default: false.
•formatProvider: The culture to use. It is often a good idea to use InvariantCulture, to make the format independent from the local culture. Default: null
I think I might need to use the formatProvider parameter to do this, but I'm not sure how I would use it.
I would expect it to interpret an empty value as 0 as well, no matter how it is configured. So this qualifies as a bug. Could you open an issue/ticket about this here? Thanks!
What am I missing:
decVal = Decimal.Parse(myAr[0]);
Or
Decimal.TryParse(myAr[0], out decVal);
Fails !
Input string was not in correct foramt.
myAr[0] is "678016".
Tried to add NumberStyle.Any and CultureInfo.InvarialtCulture but got the same results.
More info on the string:
it is concatenated with some letters in hebrew and a "\u200e" space between them. and then I use split(' ') to get the numbers back.
This is probably the source of this error, but when I check the myAr[0] in the watch it is pure string....
Guys I've found the answer, I'll rewrite the question for future generation.
The Original string was a concatenation of letters and numbers separated with a special sequence to preserve the order in a rtl situation: "\u200E".
The number where extracted later using string.split(' ') which seems to work OK (in the watch) be it caused the problem.
once I used string.split("\u200e").ToCharArray() I got the same results, but now the decimal.Parse is working.
It looks like the special char was still inside the string, invisible to the watch.
This is weird, on my machine (.NET 4) even this works:
Decimal.TryParse("asdf123&*", out someDecimal);
By works I mean that TryParse returns false, no exception is thrown.
Parse method may throw an exception - maybe you have some whitespace or string literally contains " (quotes)?
Checks if something contains any instance of any element in myString. Something may be "Sideboard: 1 Forest", "SB: 1 Mountain", "SB 1 Plains", etc. If something does contain any of the elements of the array, those elements will always be followed by a white space, a number, a white space, and a string: " 1 Swamp".
string[] myString = {"Side", "side", "Board", "board", "Sideboard", "sideboard", "SB", "sb", "SB:", "sb:"};
if(myString.Any(s => something.Contains(s)))
{
// newSomething = something but with any instance of any element in myString removed
// from the start of something up to the first whitespace.
}
I need help with the removing part of the comment in the above if statement block.
EDIT
Those are some blunt responses, but I understand!
I did go through the introduction and tried to searched for relevant stuff but couldn't find this situation. This is a little program I made for personal use, not for an assignment. I do not know anything about regular expressions.
As for clarification, at the point in the program where "something" is found to contain any of the elements in the "myString" array, I then want to remove those elements only from the start of the string in "something".
Ex: something = "Sideboard: 1 Inside Out"
I want to remove "myString" elements only from the beginning of the string and before the number because the strings after the number may also contain elements of "myString".
Also, upon submitting this question, I instantly figured out a way to do the removing: I just used TrimStart() with an array containing the individual characters of the elements in "myString". This did what I wanted but I kept this question up to look for a more elegant solution. So, for all intents and purposes, this question is already answered.
Thanks for the help!
string newSomething = something.Substring(0, something.IndexOf(' '));
The above code will return a string ending at the first instance of a space in the 'something' string. If there are no spaces in the 'something' string, there will be an exception.
Not sure if this is what you were looking for, good luck.