Jaymart Angeles Jaymart Angeles - 10 days ago 5
C# Question

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.

Answer

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