Click here to Skip to main content
15,610,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to split the string, and without using an array I need the Substring value. i.e "sheet1$" in a variable.

Sample code.
string input = "excel1.xls sheet1$";
string substrings = Regex.Split(input, ".xls");
Updated 9-Nov-11 0:47am

string substring = input.SubString(input.IndexOf(".xls ") + 5);
Share this answer
sravani.v 9-Nov-11 7:34am    
MY 5!
pradeep manne 10-Nov-11 5:15am    
hi small correction in the code that in the place of 5 we have to use 4(.xls)

updated one is
string substring = input.SubString(input.IndexOf(".xls ") + 4);
Prerak Patel 10-Nov-11 5:44am    
".xls " has 5 characters if you consider the space at the end.
pradeep manne 10-Nov-11 9:39am    
yes,am not using any space in my code ,
thank u
Ignoring the detail that that won't compile - Regex.Split returns an array of strings, not a single string. It would defeat the purpose somewhat if it didn't, wouldn't it?

I'm not positive what you are trying to split on: it looks like you want to split the string everywhere is contains the text ".xls" which seems silly, but it may be what you want to do, I don't know. If so, then you need to escape the "." as that is a regex character meaning "any character at all":
Regex.Split(input, @"\.xls")

Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
Share this answer
pradeep manne 9-Nov-11 7:07am    
my input will be similar filename.xls and sheetname
after i need only the value after .xls
string substring = input.SubString(input.IndexOf(".xls") + 4);

".xls " has 4 characters.
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