Using the C# indexOf() method and it does not work [closed] - c#

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)

Related

Why is the int.TryParse returning false everytime? [closed]

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.

Equivalent strings that look exactly the same but arn't equal in Unity [closed]

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.

How to check if a string contains any letter besides Z [closed]

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
I am trying to check to see if this object contains any letters besides Z and if it does it should return Null. The way I have it initialized gives me no errors but when testing it does not actually return null if a letter is present.
if(request.DoorTag.Contains(#"[a - yA - Y]"))
{
return null;
}
if(Regex.IsMatch(request.DoorTag, "[a-yA-Y]")
{
return null;
}
But "[^zZ]" would even be better, since it'll check that your DoorTag contains any other char than Z

Index and length must refer to a location with the string. Parameter name: length error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
My program has been receiving this error recently and I am not sure why. The code that is triggering it is:
foreach (var lot in item.LotNum.Split('|'))
{
string vendor = string.Empty;
if (lot.Trim().Contains("-"))
vendor = lot.Trim().Substring(0, item.LotNum.IndexOf("-"));
}
LotNum = "br549 | BR549 | 570-PRIOR" and lot is "570-PRIOR" (without quotes) when the error triggers. I've not used IndexOf before and so I am not sure what is wrong with the string that is being sent in. I want to check for what causes the error beforehand because the exception is stopping the program and the bad data will be there for a while until it is fixed, and more may be added in the future.
Any help would be appreciated!
New answer according your code update:
var lots = item.LotNum.Split('|');
foreach (var lot in lots)
{
string vendor = string.Empty;
if (lot.Contains("-"))
vendor = lot.Substring(0, lot.IndexOf("-")).Trim();
}
Again, you were using IndexOf for a variable different than the one you want to get a substring
You are using IndexOf for a variable different than the one you want to get a substring, so, the index will be out of range.
Try with: edited
variable = variable.Trim();
int index = variable.IndexOf("-");
if (index > 0)
variable.Substring(0, index);
Another way to do this is to check for the existence of the character using Contains, and if it's found use the IndexOf, otherwise just use the Length of the string (and put Trim at the end to avoid issues with using the Length after trimming):
variable.Substring(0, variable.Contains('-')
? variable.IndexOf('-')
: variable.Length)
.Trim();

Issue with SubString function [closed]

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 8 years ago.
Improve this question
Trying to use the .NET SubString function. I pass a value to a custom function and then an if statement evaluates if it should be capitalized or not. Then I use the following to change the first letter to upper case. However, it tells me that the "Index was outside the bounds of the array." What am I doing wrong?
char.ToUpper(X[0]) + X.Substring(1)
Wrap it in isNullOrEmpty()
if(!string.IsNullOrEmpty(X))
{
char.ToUpper(X[0]) + X.Substring(1)
}
This might help you out, as it has some sanity checks included
public string FirstLetterToUpper(string str)
{
if (string.IsNullOrEmpty(str))
return str;
return char.ToUpper(str[0]) + str.Substring(1);
}

Categories

Resources