txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
i want to find the last index of "," in my text and then remove that , but it is not working. Any Idea? txtBeautified is a richtextbox.
Are you retrieving the result of the operation?
value = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
If you are changing the value of the text box, you need to assign the result back to the text box:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Explanation: Strings cannot be changed. Functions that operate on strings do not change the strings, but return new strings. Therefore, the Remove function returns a string representing the result. To make use of this string, you will need to assign it to a variable/property or pass it into another function call.
Remove is a function. call should be:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Keep in mind that a string is immutable, so the Remove function returns you a new string. You'd need to reassign that new string back to the text box, like:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1);
Related
I have a string like
string variable1="EXAMPLE";
and later somewhere in my code, I use like
Console.WriteLine(variable1.ToLower());
I may use variable1.ToLower() multiple times. But now I want to store the variablename that is converted to Lower in a separate new variable, that is, I have to extract variable1 from Console.WriteLine(variable1.ToLower()); line and store it in a string variable. Is that possible?
My main goal is that, If my code has variable1.ToLower() in too many places, then I have to run an application, that replaces all variable1.ToLower() to a new string that has the value of variable1.ToLower(). Please Note that using too many variable1.ToLower() in a code is a violation.So I am just creating a new variable to store the value of variable1.ToLower() and use that new variable instead of variable1.ToLower() in every place.
Assuming I understand the question, why not just do this?
var lower = variable1.ToLower();
Console.WriteLine(lower);
String.ToLower creates a copy of the original string. So the original string is not modified and you can safely use it otherwise.
string variable1 = "EXAMPLE";
string lowerCaseVariable1 = variable1.ToLower();
Console.WriteLine($"Is still the original string: {variable1}");
Console.WriteLine($"Is the lower case copy of the original string: {lowerCaseVariable1}");
EDIT:
If you want to get the name of the string variable instead of the content, you can use nameof (Link).
string variable1 = "EXAMPLE";
string nameOfVariable1 = nameof(variable1);
Console.WriteLine(variable1.ToLower());
Lets say I have a String Array full of items such as:
string[] letters = new string[4] {"A1","B1","C1","D1"};
Later, I want to set the contents of a textbox to the first value in the array:
Letter.Content = letters[0];
Is there a way to 'clip' the number out of the String in the Array? For example, in my above code, currently the Letter textbox would be set to 'A1'. What I want however is to set it to just 'A'.
Depends on if the strings's length is always two and the digit is at the second position. Then it's simple:
Letter.Content = letters[0][0];
If you don't know the length but you want to take all letters from the left until there is a non-letter you could use string.Concat + LINQ:
Letter.Content = string.Concat(letters[0].TakeWhile(Char.IsLetter));
or you could do it the old fashion way using SubString Method
Letter.Content = letters[0].Substring(0,1);
scrollerList = new List<string>(TextExtractor.newTextWithoutLinks);
scrollerText = string.Join(Environment.NewLine, scrollerList);
combindedString = string.Join(Environment.NewLine, newText);
scroller1.TextToScroll = scrollerText;
richTextBox1.Text = combindedString;
richTextBox1.Text = richTextBox1.Text.TrimStart();
richTextBox1.Refresh();
This is a working line:
richTextBox1.Text = richTextBox1.Text.TrimStart();
But if i'm doing:
richTextBox1.Text.TrimStart();
It's not working i mean dosen't make any changes.
Not that i'm getting any exceptions but a bit strange i need to assign twice to the richTextBox1 the text to delete the empty line at the top of the richTextBox1.
string is immutable - what you're seeing is expected behaviour. Operations like TrimStart() will create a new string, which is returned when calling that method.
Why not do it in one go?
richTextBox1.Text = combindedString.TrimStart();
TrimStart() returns a new string with the values trimmed. It does not modify the original string:
From http://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx
This method does not modify the value of the current instance.
Instead, it returns a new string in which all leading white space
characters found in the current instance are removed.
It generates a new string, which has nothing to do with the old one. You have to replace the existing one with the new one!
So this is right:
richTextBox1.Text = richTextBox1.Text.TrimStart();
I think this should be a pretty easy question to answer but I can't seem to figure it out.
I am adding text to labels from a sqldatasource in c#. All of that works, but I want to be able to format the text. I want to 1) be able to change the format to 0.00 (instead of a string of decimals) and I would also like to be able to add words before the text. I assume I need to somehow use the string.format command but can't figure out how to work it in. Any help would be greatly appreciated. Here's my code below:
DataView dvSql = (DataView)DeskSummary.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
Desk.Text = drvSql["Deskname"].ToString();
MarginLabel.Text = drvSql["margin"].ToString();
CurrentCI.Text = drvSql["comp_index_primarycomp"].ToString();
WalMartCurrentCI.Text = drvSql["comp_index_walmart"].ToString();
ForecastMargin.Text = drvSql["margin_forecast"].ToString();
WalMartForecastCI.Text = drvSql["comp_index_walmart_forecast"].ToString();
ForecastCI.Text = drvSql["comp_index_primarycomp_forecast"].ToString();
}
You can pass the format argument to the ToString() method like so:
MarginLabel.Text = drvSql["margin"].ToString("0.00");
However, as you said you wanted to prepend some text. Therefore, I recommend:
MarginLabel.Text = String.Format("Prepended text {0:0.00}", drvSql["margin"]);
Note: I just picked one of your labels; I'm not sure which ones get special formatting treatment.
use the
string.Format("This is a before text {"0"},your param)
// you can add as many variables and {""} string literals as you need just make sure that you separate the variables with a ","
Here is the code
string stringNumber = "5123.34214513";
decimal decimalNumber = Decimal.Parse(stringNumber);
string output = String.Format("Your text: {0:0.00}", decimalNumber);
Console.WriteLine(output); //Your text: 5123.34
This works if the column is of type string
String.Format() will do what you need for prepending/appending text values,
string.Format("prepend text {"0"} append text", paramString)
But if you want to actually format the value you are getting back from SQL, then you would need to use String.Format() on that value as well as possibly some RegEx expressions and/or .ToUpperCase or .ToLowercase for your capitalization... something like.
var capitalizedString = paramString.subStr(0,1).ToUppercase + paramString.subStr(1, paramstring.Length);
string.Format("Prepended text {"0"} plus appended text", capitalizedString);
My strings look like that: aaa/b/cc/dd/ee . I want to cut first part without a / . How can i do it? I have many strings and they don't have the same length. I tried to use Substring(), but what about / ?
I want to add 'aaa' to the first treeNode, 'b' to the second etc. I know how to add something to treeview, but i don't know how can i receive this parts.
Maybe the Split() method is what you're after?
string value = "aaa/b/cc/dd/ee";
string[] collection = value.Split('/');
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.
Based on your updates related to a TreeView (ASP.Net? WinForms?) you can do this:
foreach(string text in collection)
{
TreeNode node = new TreeNode(text);
myTreeView.Nodes.Add(node);
}
Use Substring and IndexOf to find the location of the first /
To get the first part:
// from memory, need to test :)
string output = String.Substring(inputString, 0, inputString.IndexOf("/"));
To just cut the first part:
// from memory, need to test :)
string output = String.Substring(inputString,
inputString.IndexOf("/"),
inputString.Length - inputString.IndexOf("/");
You would probably want to do:
string[] parts = "aaa/b/cc/dd/ee".Split(new char[] { '/' });
Sounds like this is a job for... Regular Expressions!
One way to do it is by using string.Split to split your string into an array, and then string.Join to make whatever parts of the array you want into a new string.
For example:
var parts = input.Split('/');
var processedInput = string.Join("/", parts.Skip(1));
This is a general approach. If you only need to do very specific processing, you can be more efficient with string.IndexOf, for example:
var processedInput = input.Substring(input.IndexOf('/') + 1);