Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
sry for yesterday's question..

There is a string named as "something.docx". How to split that string using Regular Expression.

Also find how modify the ".docx" extension,means how it can be replace with ".pdf".
How to find the reverse of string, means In "something.docx", you may come from back onwards, and find the first .(dot) and split the string upto that .(dot) and capture the front string means "something" and add ".pdf" extensions.

Note: You consider "something.docx" as a string, not a document and also its not converted into pdf format. just replace .docx with .pdf
Posted
Comments
ambarishtv 27-Apr-11 3:45am    
string result = Regex.Replace("Sample.docx", "docx", "pdf");

This article[^] would give you a brief idea of how powerful regular expressions can be and how they could be used to split strings.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 12:24pm    
Well, a 5 for the reference but... No!
Nothing really to do with Regex -- I feel like this is a Regex season in some school(s), no less :-)
Please see my answer for really practical approach.
--SA
Abhinav S 27-Apr-11 12:26pm    
Thanks for the 5. I shall accept your comment :).
Tarun.K.S 27-Apr-11 14:27pm    
Good link and bookmarked. 5+
Abhinav S 27-Apr-11 23:16pm    
Thanks Tarun.
You don't need Regular expressions here.

Look at the class System.IO.Path. How about System.IO.Path.ChangeExtension and System.IO.Path.GetExtension, System.IO.Path.GetFileName and System.IO.Path.GetFileNameWithoutExtension? It will cover all you need. See:
http://msdn.microsoft.com/en-us/library/system.io.path.aspx[^].

After all, you have string.Split.

A final note: nothing "manipulates" strings — they are immutable. All methods only read string data and created other data from scratch. Practically, this is good to know, for performance and other reasons.

—SA
 
Share this answer
 
Comments
Abhinav S 27-Apr-11 12:26pm    
5 for the alternate approach.
Sergey Alexandrovich Kryukov 27-Apr-11 15:22pm    
Thank you, Abhinav.
--SA
Olivier Levrey 27-Apr-11 12:29pm    
I agree. Simple tools should be enough for simple tasks. My 5 SA.
Sergey Alexandrovich Kryukov 27-Apr-11 15:22pm    
Exactly!
Thank you, Olivier.
--SA
Tarun.K.S 27-Apr-11 14:27pm    
Good answer. 5+

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