Click here to Skip to main content
15,559,819 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:

I want to split a string based on position number.
for ex:
i have a string like "this is good"
Then i want the string from 2nd position to 5th position
Could anyone help on this.

Hiren solanki 19-Jan-11 7:55am    
Simply substring can achieve that.

I think you mean to say you want the portion of the string (or what is commonly called a "substring").

string x = "this is good";
string y = x.Substring(1,4);

The variable y should contain "his ".
Share this answer
fjdiewornncalwe 19-Jan-11 17:56pm    
Nice, but you perhaps should have included information about 0 based indexing... (+5)... (Kidding about the 0 based, of course)
There are thousands of links available on the Net.,
Simple Google Search is solve your Problem.

Try in Google / CP Before Posting Question Here.

Google is your Friend.:rose::rose:
Share this answer
Sergey Alexandrovich Kryukov 19-Jan-11 9:23am    
Rajesh, your answer is the best on this page (my 5).
(You could even better advice using regular help though.) Asking such questions indicate amazing degree of laziness and impudence, so down-voting like of answers like that is natural. Why not ignoring them (not for the sake of score, of course)?
Use substring function
string str="this is good";
string s0 = str.Substring(1, 4);

have a look for more
Share this answer
#realJSOP 19-Jan-11 7:57am    
He said 2nd position. Since character arrays are 0-based, that should be "Substring(1, 4)".
m@dhu 19-Jan-11 8:02am    
Yes my mistake updated sir.
#realJSOP 19-Jan-11 8:50am    
So how is your response any different from mine?
m@dhu 19-Jan-11 8:55am    
I actually didn't refresh before posting else I wouldn't have post the same.[^]

Don't use System.String's substring when string's substr will suffice. The System.String class is a lot slower.

Just noticed this is a C# question. In that case you might use SubString for convenience.
Share this answer

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