6

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?

2
  • 5
    Advice for future make some research for your own before you ask such a question:) Commented 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. Commented Jul 20, 2012 at 8:38

4 Answers 4

10
// 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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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.
5
// 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

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?
@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);
1

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

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

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

If going for a wrapper around SubString, I would recommend creating a Extension method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.