Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string with name like this (VAN909_445_Viperacrfd_40450.jpg)

Now i want to remove the part (Viperacrfd_40450.jpg). The 445 is an id and i'll be able to retrieve from DB so that triming will be easier.. Can anyone tell me how to get the desired result of Viperacrfd_40450.jpg
Posted
Comments
DamithSL 4-Jul-12 5:00am    
you want only "Viperacrfd_40450.jpg" or final result you want is "VAN909Viperacrfd_40450.jpg"?
DamithSL 4-Jul-12 5:02am    
"string1_string2_string3" is this the format of your input? if Yes, what is the output you expect?
Arjun Menon U.K 4-Jul-12 5:06am    
Damith the final result i want is Viperacrfd_40450.jpg
DamithSL 4-Jul-12 5:48am    
check my answer, if you are using .net 3.0 + version then you can do this as my answer

like this:

C#
string input = "VAN909_445_Viperacrfd_40450.jpg";
int Id = 445;

int index = input.IndexOf(string.Format("_{0}_", Id), 0);
string result = input.Remove(0, index + 2 + Id.ToString().Length);
 
Share this answer
 
Comments
Arjun Menon U.K 4-Jul-12 5:10am    
Hey Manas i tried like this

string attachmentName;

attachmentName = dsTicketDetails.Tables[0].Rows[i]["Attachment"].ToString().Substring(dsTicketDetails.Tables[0].Rows[i]["Attachment"].ToString().IndexOf(dsTicketDetails.Tables[0].Rows[i]["attachmentID"].ToString()));

attachmentName = attachmentName.Substring(attachmentName.IndexOf("_")+1);
Arjun Menon U.K 4-Jul-12 5:19am    
Please let me know if there is any problem
have a look at Regular Expressions in C#[^]
 
Share this answer
 
Hi,
you can use split function.
then concatinate the result according to your need.
 
Share this answer
 
.net 3.0 or above, using Linq
C#
string input =  dsTicketDetails.Tables[0].Rows[i]["Attachment"].ToString();
string result =string.Join("_",  input.Split('_').Skip(2));
 
Share this answer
 
v2
So we have the string below as:

VAN909_445_Viperacrfd_40450.jpg

Assuming that VAN909 is always the same at the beginning and if you can get the id, can you not do something like:

C#
string actualText = "VAN909_445_Viperacrfd_40450.jpg";

string id = 445; //Whatever ID you get over here
string textToRemove = "VAN909_" + id + "_"; //Assuming that VAN909_ is the beginning string always

string newText = actualText.Replace(textToRemove, "");



That should do it if the assumptions were used.
 
Share this answer
 
Comments
Arjun Menon U.K 4-Jul-12 7:47am    
Nithin the name is dynamic.. everything changes

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