1

I have a sample textbox txtAddress and I want to save it on two string address1, address2. if txtAddress is more than 15 character the other character will store in address2. example:

txtAddress = 'how can i save this string'

storing should be, address1='how can i save, address2= ' this string'

i try this code but i dont know how to split using delimiter of number of character

txtAddress.Value= "how can i save this string";
  Char delimiter = 15;
  String[] substrings = txtAddress.Value.Split(delimiter);
  foreach (var substring in substrings)

Thanks in advance, hope you can help me guys.

1
  • it works, thanks a lot Sir. Commented Nov 23, 2016 at 3:43

2 Answers 2

3

Substring might help you acheive it as this method extracts strings. It requires the location of the substring (a start index, a length). It then returns a new string with the characters in that range.

string txtAddress = "how can i save this string";
if(txtAddress.Length >= 15)
{
    string address1 = txtAddress.Substring(0, 15);
    string address2 = txtAddress.Substring(15);
    Console.WriteLine(address1 + " -#- " + address2);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hope that you missed few things, this code wont compile. could you please take a look?
From the question it is clear that txtAddress is a Textbox it doesn't contains definition for Substring(), you should use its .Text property(or .Value according to the type of textbox that you are using) to get the Text from it
Okay @un-lucky I got your point. Lemme fix that with another way.
I store the value of textbox and use this code and it works, thanks Sir.
0

An ideal approach would be using different textboxes for address1 and address2. Anyways, you can split the string using String.Split method. For example:

string s = "address1, address2";
  Char delimiter = ',';
  String[] substrings = s.Split(delimiter);

Comments

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.