Reputation: 81
I have a string of attachments like this:
"
<a href="/departments/Attachments/2043_3162016062557_SharePoint_Health%20Check%20Assessment.docx">SharePoint_Health Check Assessment.docx</a><br><a href="/departments/Attachments/2043_3162016062557_Test%20Workflow.docx">Test Workflow.docx</a><br>
" .
and i used this method :
AttachmentName = System.Text.RegularExpressions.Regex.Replace(AttachmentName, @"<(.|\n)*?>", "String.Empty");
and i got result :
SharePoint_Health Check Assessment.docxTest Workflow.docx
How can i split the string using c# and get the result with each file name seperately like :
SharePoint_Health Check Assessment.docx
Test Workflow.docx
and then show them into some control one by one.
and after that i want just the URL of the string like "http://srumos1/departments/Attachments/2053_3172016093545_ITPCTemplate.txt" and "http://srumos1/departments/Attachments/2053_3172016093545_ITPCTemplate.txt"
how can i do that
Upvotes: 0
Views: 220
Reputation: 152634
i got it this way
AttachmentName = Regex.Replace(AttachmentName, @"<(.|\n)*?>", string.Empty);
Well there's your problem. You had valid delimiter but stripped them away for some reason. Leave the delimiters there and use String.Split
to split them based on that delimiter.
Or replace the HTML with a delimiter instead of an empty string:
AttachmentName = Regex.Replace(AttachmentName, @"<(.|\n)*?>", "|");
And then split based off of that:
string[] filenames = AttachmentName.Split(new [] {'|'},
StringSplitOptions.RemoveEmptyEntries);
Upvotes: 2
Reputation: 2138
You can use a regex for extracting file names if you do not have any other clear way to do that. Can you try the code below ?;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
namespace ExtensionExtractingTest
{
class Program
{
static void Main(string[] args)
{
string fileNames = "test.docxtest2.txttest3.pdftest.test.xlxtest.docxtest2.txttest3.pdftest.test.xlxtest.docxtest2.txttest3.pdftest.test.xlxourtest.txtnewtest.pdfstackoverflow.pdf";
//Add your extensions to regex definition
Regex fileNameMatchRegex = new Regex(@"[a-zA-Z0-9]*(\.txt|\.pdf|\.docx|\.txt|\.xlx)", RegexOptions.IgnoreCase);
MatchCollection matchResult = fileNameMatchRegex.Matches(fileNames);
List<string> fileNamesList = new List<string>();
foreach (Match item in matchResult)
{
fileNamesList.Add(item.Value);
}
fileNamesList = fileNamesList.Distinct().ToList();
Console.WriteLine(string.Join(";", fileNamesList));
}
}
}
And a working example is here http://ideone.com/gbopSe
PS: Please keep in mind you have to know your file name extensions or you have to predict filename extension length 3 or 4 and that will be a painful string parsing operation.
Hope this helps
Upvotes: 2