简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM