Stuck in a loop updating a value [duplicate] - c#

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have an application that updates values in documents, however, some of these documents have multiple entries of this value. due to this I have created a Do Something loop but this is just looping and is not replacing the values.
my code is as below:
do
{
int dollarIndex = script.IndexOf("$");
string nextTenChars = script.Substring(dollarIndex - 17, 17);
string promptValue = CreateInput.ShowDialog(nextTenChars, "Input");
script.Replace("$", promptValue);
}
while (script.Contains("$"));

Strings are immutable, so you need to do:
script = script.Replace("$", promptValue);
Simply doing
script.Replace("$", promptValue);
Doesn't update the value of script

Related

C# Check if any string contains in string array and get the value [duplicate]

This question already has answers here:
Find index of a value in an array
(8 answers)
Getting the index of a particular item in array
(5 answers)
Closed 2 years ago.
I am new in c# and stucked with getting value from arrays. I can check if the string contains any string in array but i have no idea how to get the value of matched string.
In my code i want to get "When is" as a string.
string testwords = "When is your birthday";
string[] myStrings = { "Who is ", "When is ", "What is " };
if(myStrings.Any(testwords.Contains))

c# convert string to array on char(253) deliminator [duplicate]

This question already has answers here:
How do i split a String into multiple values?
(5 answers)
Closed 3 years ago.
Not entirely sure the following code is going to help many people, but here goes
try
{
uvConnect = UniObjects.OpenSession(serverId, sUser, sPass, sAcct, "uvcs");
// Open Movie File
UniFile uvFile = uvConnect.CreateUniFile("MOVIES");
UniDynArray movieRec = uvFile.Read(txtMovieId.Text);
string sMovieData = movieRec.StringValue;
MessageBox.Show(sMovieData);
}
sMovieData contains a single string of the entire record retrieve from MOVIES file, each field is deliminated by a char(253) character in the database I am using.
Is there a function/method/etc to convert the string to an array using char(253) as a value deliminator
Something like this should work:
string[] fields = sMovieData.Split((char)253);
Try this... string[] arrayValues = "stringToConvertToArray".Split((char)253);

How to check if List contains part of a value? [duplicate]

This question already has answers here:
Find substring in a list of strings
(6 answers)
Closed 6 years ago.
I was wondering if it possible to check if a List contains part of a value. If it finds the value then return the value.
E.g. If the List had values 12345, 14567 and 14785, I want to search if the List contains '123',
Is this possible?
If it is can all values that contain '123' be returned?
This is how I add values to the List:
recordFailedPO.Add(Convert.ToInt32(dataGWHeight.Rows[0].Cells[0].Value));
This is how I'm checking for a part of a value:
if (recordFailedPO.Contains(currentPO))
{
// Code Here
}
Where currentPO is the user input.
Thanks for any help
In that case you could use string Contains method
var containsNumber = recordFailedPO.Where(x => x.ToString().Contains("123"));
try to convert to string before
int res = recordFailedPO.Find(x=>x.ToString().Contains(currentPO));
All values can be returned by FindAll .
List<int> res = recordPO.FindAll(x=>x.ToString().Contains(currentPO)):

textbox.Text using index array in c# [duplicate]

This question already has answers here:
how do I set a character at an index in a string in c#?
(7 answers)
Closed 8 years ago.
example:
textbox1.Text[0]="a";
textbox1.Text[1]="s";
so,the text appear in textbox1 is "as"
is there a way to do that?
No, strings are immutable. You can't manipulate strings like that, you need to create a new string and assign it to your Text property. You can assign it directly:
textBox1.Text="as";
Or you can use a StringBuilder:
var builder = new StringBuilder();
builder.Append("a");
builder.Append("s");
textBox1.Text = builder.ToString();
textbox1.Text="a";
And than, to add more characters to that string use
textbox1.Text +="s";

C# equivalent of Python repr() [duplicate]

This question already has answers here:
Can I convert a C# string value to an escaped string literal?
(16 answers)
Closed 9 years ago.
Is there a C# method like Python's repr() to get the true representation of the object? Suppose we have:
string identifier = "22\n44";
Console.WriteLine(identifier);
This would return
22
44
Is there a way to get
"22\n44"
In Python this is easy. We can just do repr("22\n44").
I thought of this question because I was trying to convert "2244" to '2244' using
var identifier = "2244";
identifier = identifier.Replace("\"", "'");
Console.WriteLine(identifier);
The output is just 2244, because double quotes are for our purpose. But in my case, I did this to get what I wanted:
identifier = string.Format("'{0}'", identifier);
because initially the database was receiving the query as IN ("2244") instead of IN ('2244') and was throwing Invalid Number error.
string identifier = "22\n44";
Console.WriteLine("\"{0}\"", identifier.Replace("\n", #"\n"));

Categories