Jaymart Angeles
Jaymart Angeles

Reputation: 35

How can I split textbox value and store it in two string variable c#

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.

Upvotes: 1

Views: 3460

Answers (2)

Mohit S
Mohit S

Reputation: 14064

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

Upvotes: 3

Ashish
Ashish

Reputation: 11

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

Upvotes: 0

Related Questions