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

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";

Related

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);

c# - How can i format a string in c# [duplicate]

This question already has answers here:
Add zero-padding to a string
(6 answers)
Closed 4 years ago.
I have a very simple Question to ask.
I have a string like:
string str="89";
I want to format my string as follow :
str="000089";
How can i achieve this?
Assuming the 89 is actually coming from another variable, then simply:
int i = 89;
var str = i.ToString("000000");
Here the 0 in the ToString() is a "zero placeholder" as a custom format specifier; see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you have a string (not int) as the initial value and thus you want to pad it up to length 6, try PadLeft:
string str = "89";
str = str.PadLeft(6, '0');
If you want the input to be a string you'll have to parse it before you output it
int.Parse(str).ToString("000000")

c# replace character by an empty string [duplicate]

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'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

Stuck in a loop updating a value [duplicate]

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

How to get 4 digits from the middle a string in C# [duplicate]

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 8 years ago.
I have a string variable like test10015, i want to get just the 4 digits 1001,
what is the best way to do it?
i"m working in asp.net c#
With Linq:
var expected = str.Skip(4).Take(4);
Without Linq:
var expected = str.Substring(4,4);
Select the first four digits in your string:
string str = "test10015";
string strNum = new string(str.Where(c => char.IsDigit(c)).Take(4).ToArray());
You can use String.Substring Method (Int32, Int32). You can subtract 5 from from the length to start from your required index. Make sure the format of string remains the same.
string res = str.Substring(str.Length-5, 4);
string input = "test10015";
string result = input.Substring(4, 4);

Categories