String replace not working in function [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
string input= "hello, how are you tod&#97y"
i knw the encode is not in proper format thats why i am using a function to replace improper "tod&#97y" to "today".
f1(input);
but at time of comparing
if (input.Contains("today") == true)
{
lbldisplay.Text = str1;
}
it returing false,i have debugged the program.it is working correctly till replace s1.Replace("&#97","a");(shown "hello, how are you today") but at return statement return s1; it is returning original value i.e "hello, how are you tod&#97y".
public string f1(string s1)
{
s1 = s1.Replace("&#97", "a");
return s1;
}
please help.thank you.

Most likely what is happening is that you're not assigning the return value back to your variable when you call it. The parameter is not declared ref so this will have no effect:
f1(input);
You would need to use this:
input = f1(input);

Related

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

IEnumerable<T> Queue.Count is not returning an integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I am attempting to define a queue named movingAverages with a size of queue.Count - period. I am getting an error "int IEnumerable.Count() ... - cannot be applied to method and int....
private static IEnumerable<DateClose> MovingAverage(
IEnumerable<DateClose> queue, int period)
{
Queue<DateClose> movingAverages = new Queue<DateClose>(queue.Count + period);
return movingAverages;
}
Well it's because IEnumerable<T>.Count is a method, so you are missing the parentheses at queue.Count + period, which should be queue.Count() + period.

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