Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I assign string value in string array. Is split method required to do this. If so, Why do we have to use Split() Method.
Posted
Comments
Mehdi Gholam 23-Apr-13 4:22am    
Show what you want to do in code as your question does not make sense.

Hi,

You need split function because you need to specify on what basis you want to convert string into array. example : you have comma separated mobile number and if you want to convert it into array then you need to split with the comma.

Let us know what is your exact query with code.

best luck.
 
Share this answer
 
Comments
Sumon562 23-Apr-13 4:46am    
I have posted a question before this. Please answer that
AmitGajjar 23-Apr-13 4:52am    
Please post some code so we can give you exact answer.
You can't "assign a string value in a string array" - you can assign a strign vlaue to an element of a string array, but you can only assign arrays to arrays and vice versa.
So you can do this:
C#
string s = "Hello there";
string[] arr = new string[5];
arr[2] = s;
But you can't do this:
C#
string s = "Hello there";
string[] arr = s;
because the types are not compatible.

You can use Split to break a string into an array of substrings:
C#
string s = "Hello there";
string[] arr = s.Split(' ');

which will give you an array of two strings, containing "hello" and "there".
This is very useful when separating user input strings for example:
C#
string s = "Smith;John;2 Letsbe Avenue, Luton";
string[] arr = s.Split(';');
would break the input into the various address parts.
 
Share this answer
 
Comments
AmitGajjar 23-Apr-13 4:41am    
Perfect with different example. 5+
Sumon562 23-Apr-13 4:47am    
Thank you. Please give the answer that I posted here before this question. I will be very regretful if I get it.
OriginalGriff 23-Apr-13 4:52am    
Sorry? I do not understand what you are saying. It's probably a language problem, or a lack of coffee on my part, but please try again. Perhaps if you write it in your native language and use Google Translate to convert it to English?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900