Click here to Skip to main content
15,891,184 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.
C#
string input = "excel1.xls sheet1$";
string substrings = Regex.Split(input, ".xls");
Posted
Updated 9-Nov-11 0:47am
v3

C#
string substring = input.SubString(input.IndexOf(".xls ") + 5);
 
Share this answer
 
Comments
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":
C#
Regex.Split(input, @"\.xls")


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
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
 
v3

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