I'm trying to copy part of the text from text box to another string. For example if my textbox contain 10 characters, I want to copy from character 3 to character 7 to another string call TEST. How do we do it?
-
5Advice for future make some research for your own before you ask such a question:)Harry89pl– Harry89pl2012-07-20 08:34:09 +00:00Commented Jul 20, 2012 at 8:34
-
Are you sure you just want to copy characters like this and nothing smarter than that? Usually you want to copy whole words like the second, third and fourth word.Eric Herlitz– Eric Herlitz2012-07-20 08:38:16 +00:00Commented Jul 20, 2012 at 8:38
Add a comment
|
4 Answers
// when textbox contains "ABCDEFGHIJ", the result will be "CDEFG"
string result = textBox.Text.Substring(2, 5);
Keep in mind that this will throw an exception for strings shorter than 7 characters, so you may want to add some length checks.
2 Comments
maniac84
Thanks. The important thing is to know the command SubString. By the way, is there a way to skip past some desired character. Like for example if the data is 07/20/2012, but I just want to copy 07202012 and ignore the '/'. How do we do it?
Nikola Anusev
@maniac84
"07/20/2012".Replace("/", string.Empty). Try checking out MSDN and especially methods defined on string - you will get what you want much faster.// Start at the 2nd index (0=based index)
// Take from the 3rd to the 7th character,
string test = textBox.Text.Substring(2, 5);
2 Comments
maniac84
Thanks. The important thing is to know the command SubString. By the way, is there a way to skip past some desired character. Like for example if the data is 07/20/2012, but I just want to copy 07202012 and ignore the '/'. How do we do it?
Myrtle
@maniac84 You can remove characters from a string using
string test = test.Replace("/", ""); however if it is a date I would suggest to use DateTime dateTime = DateTime.Parse(textBox.Text);I think the method you are looking for is Substring. With this method you can get any part of a string starting form a certain index.
For Example:
string test = YourTextBox.Text.Substring(2, 5);
In this example you'll get foru characters of the string in YourTextBox starting at index 2.
2 Comments
maniac84
Thanks. The important thing is to know the command SubString. By the way, is there a way to skip past some desired character. Like for example if the data is 07/20/2012, but I just want to copy 07202012 and ignore the '/'. How do we do it?
Daniel Schäffel
You could simply use the method Replace(). With this method you can replace a certain character or string wiht something else. If you want te remove them completely you can simply use string.Empty (e.g. YourTextBox.Text.Replace("/", string.Empty); )
Here you go
string test = TakePieceFromText("this is my data to work with", 2, 5);
/// <summary>
/// Takes the piece from text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="length">The length.</param>
/// <returns>a piece of text</returns>
private string TakePieceFromText(string text, int startIndex, int length)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException("no text givin");
string result = string.Empty;
try
{
result = text.Substring(startIndex, length);
}
catch (Exception ex)
{
// log exception
}
return result;
}
1 Comment
Myrtle
If going for a wrapper around
SubString, I would recommend creating a Extension method.