Merge two EmailMessage in C# - c#

What is the correct way to merge two EmailMessage. I tried following:
mergedMessage.Body.Text = message1.Body.Text + message2.Body.Text
But it is creating two html tags in the merged message, which is not correct.
Should I parse the message1.Body.Text and message2.Body.Text and fetch the content of html and copy to the mergedMessage?

You can do this
using System.Text.RegularExpressions;
const string HTML_TAG_PATTERN = "<.*?>";
static string StripHTML (this string inputString)
{
return Regex.Replace
(inputString, HTML_TAG_PATTERN, string.Empty);
}
mergedMessage.Body.Text = message1.Body.Text.StripHTML() + message2.Body.Text.StripHTML()

Related

how to Convert HTML characters like #amp; to their Proper Form in C#

How to convert these characters to plain text?
â„¢,  ®, â„¢, ® and —
this problem occurs when I get a text from the website during scraping and store it into the database.
But it adds special characters and & like character.
I want to remove these all.
you can use this:
Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(myvalue));
try this:
public static string RemoveUTFCharactes(this string input)
{
string output = string.Empty;
if (!string.IsNullOrEmpty(input))
{
byte[] data = System.Text.Encoding.Default.GetBytes(input);
output = System.Text.Encoding.UTF8.GetString(data);
}
return output;
}
The short solution of your question is below
if you have limited symbols you can use the Replace method in C# language, like this
string symbol="this is the book #amp; laptop";
string formattedterm = symbol.Replace("#amp;","&");

string.split parse url from large string

How would I parse and extract the url from this large string of text in the image below?
I want the .m3u8 link.
I tried using String.split() but that only accepts chars and not strings.
I dont know this approach would be right but I guess you can remove those lines containing "EXT" if they are common in your url.
var result = url.Split(new [] { '\r', '\n' }); // converting string to lines
for (int i=0;i<=result.Length-1;i++) // Finding if EXT text is present and removing them
{
if (result[i].Contains("EXT-")
result.RemoveAt(i);
}
string final = string.Join("", result); // converting back to string
Tell me if it works for you!
How would I parse and extract the url from this large string of text?
use this code:
void Main()
{
var text = #"
otEXT-X-MEDIA:TYPE=VIDEO,GROUP-ID=720p30',NAME=*720p°,AUTOSELECT=YES,DEFAULT=YESEXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2374703,RESOLUTION=1280x720,CODECS=•avc1.77.31,mp4a.40.2',VIDEO=720p30'
http://video-weaver.lax03.hls.tbmw.net/v1/playlist/CuoCf3YooUjSfbk71Zs3ig68T2vDVg7449mAN78mSV1oQ9V7skuTh6NP9FOSaIgGNgnpliscdDC9XvjGyGcYrIXBbAXSkqCGuIOTQgtIw5IQLUIbHGMZIXWZeD6Urd4GzyPxCoARIYyo6i5ECCfK2r1jW1kidXGotcRXy6fHolJw-rC9xPPYc2IxYKaERQ9NsOMGn3m2ChqWFSpRHbYNK1M8OrU4WecMwgoFkORbEBEmAmng2V8GnGz63hWNV2sDW1H9E5pYjG4jpPLEf_Fwt75CePIgyZ9g30Kgr5CHZYSyMMbMAX-eac5wC3wjVUWtGz094t4xH1713yvWjv813vCY6NRBCPkCINdpXBmUnLcnE1JOLye_NiGx5R1B4IMDpRXDZQAO6PBm97ZNhyLZKc9Awg5vypphWG2MMAGdboca5WGtG_wVRp12SiHw9n0a51VpqNjVVVWbASuUly-CEe22tzLkwEolOWGE8VQSECyPx17qBU7YPHCEEi8ncnMaDly1Dn8j0xU-QN71kA.m3u8
";
MatchCollection ms = Regex.Matches(text, #"(www.+|http.+)([\s]|$)");
string url = ms[0].Value.ToString();
Console.WriteLine(url);
}
I tried using String.Split but that only accepts chars and not strings.
this question you can use extension
but it's can't deal your problem
void Main()
{
var extention_str = "http://aaacccddd".Split("ccc");
}
public static class StrinExtention {
public static string [] Split(this string str,string separator){
return str.Split(new string [] {separator},StringSplitOptions.None);
}
}

How to extract email from html link

Hi I have a csv file which I need to format (columns) email, they are in the csv as follows
john#domain.com"
dave.h#domain22.co.uk"
etc...
So i want to remove " and just use john#domain.com
I have the following
foreach (var clientI in clientImportList)
{
newClient = new DomainObjects.Client();
//Remove unwanted email text??
newClient.Email = clientI.Email
}
I would suggest to use HtmlAgilityPack and not parse it yourself:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[#href"])
{
string href = link["href"].Value;
// use "mailto:john#domain.com" here..
}
You can test regular expressions here:
https://regex101.com/
Using your example, this seems to work:
mailto:(.*?)\\">
The library needed for regex is:
using System.Text.RegularExpressions;
I usually write myself little utility classes and extensions to handle things like this. Since this probably won't be the last time you have to do something like this you could do this:
Create an Extension of the string class:
public static class StringExtensions
{
public static string ExtractMiddle(this string text, string front, string back)
{
text = text.Substring(text.IndexOf(front) + 1);
return text.Remove(text.IndexOf(back));
}
}
And then do this(Could use better naming, but you get the point):
string emailAddress = text.ExtractMiddle(">", "<");
If you want to do it the index way, something like:
const string start = "<a href=\\mailto:";
const string end = "\\\">";
string asd1 = "john#domain.com\"";
int index1 = asd1.IndexOf(start);
int startPosition = index1 + start.Length;
int endPosition = asd1.IndexOf(end);
string email = asd1.Substring(startPosition, endPosition - startPosition);

Find & Replace Multiple Instagram Urls In A String Using C#

I want to find all the instagram urls within a string, and replace them with the embed url.
But I'm keen on performance, as this could be 5 to 20 posts each anything up to 6000 characters with an unknown amount of instagram urls in which need converting.
Url examples (Could be any of these in each string, so would need to match all)
http://instagram.com/p/xPnQ1ZIY2W/?modal=true
http://instagram.com/p/xPnQ1ZIY2W/
http://instagr.am/p/xPnQ1ZIY2W/
And this is what I need to replace them with (An embedded version)
<img src="http://instagram.com/p/xPnQ1ZIY2W/media/?size=l" class="instagramimage" />
I was thinking about going for regex? But is this the quickest and most performant way of doing this?
Any examples greatly appreciated.
Something like:
Regex reg = new Regex(#"http://instagr\.?am(?:\.com)?/\S*");
Edited regex. However i would combine this with a stringreader and do it line by line. Then put the string (modified or not) into a stringbuilder:
string original = #"someotherText http://instagram.com/p/xPnQ1ZIY2W/?modal=true some other text
some other text http://instagram.com/p/xPnQ1ZIY2W/ some other text
some other text http://instagr.am/p/xPnQ1ZIY2W/ some other text";
StringBuilder result = new StringBuilder();
using (StringReader reader = new StringReader(original))
{
while (reader.Peek() > 0)
{
string line = reader.ReadLine();
if (reg.IsMatch(line))
{
string url = reg.Match(line).ToString();
result.AppendLine(reg.Replace(line,string.Format("<img src=\"{0}\" class=\"instagramimage\" />",url)));
}
else
{
result.AppendLine(line);
}
}
}
Console.WriteLine(result.ToString());
You mean like this?
class Program
{
private static Regex reg = new Regex(#"http://instagr\.?am(?:\.com)?/\S*", RegexOptions.Compiled);
private static Regex idRegex = new Regex(#"(?<=p/).*?(?=/)",RegexOptions.Compiled);
static void Main(string[] args)
{
string original = #"someotherText http://instagram.com/p/xPnQ1ZIY2W/?modal=true some other text
some other text http://instagram.com/p/xPnQ1ZIY2W/ some other text
some other text http://instagr.am/p/xPnQ1ZIY2W/ some other text";
StringBuilder result = new StringBuilder();
using (StringReader reader = new StringReader(original))
{
while (reader.Peek() > 0)
{
string line = reader.ReadLine();
if (reg.IsMatch(line))
{
string url = reg.Match(line).ToString();
result.AppendLine(reg.Replace(line, string.Format("<img src=\"http://instagram.com/p/{0}/media/?size=1\" class=\"instagramimage\" />", idRegex.Match(url).ToString())));
}
else
{
result.AppendLine(line);
}
}
}
Console.WriteLine(result.ToString());
}
}
A well-crafted and compiled regular expression is hard to beat, especially since you're doing replacements, not just searching, but you should test to be sure.
If the Instagram URLs are only within HTML attributes, here's my first stab at a pattern to look for:
(?<=")(https?://instagr[^">]+)
(I added a check for https as well, which you didn't mention but I believe is supported by Instagram.)
Some false positives are theoretically possible, but it will perform better than pedantically matching every legal variation of an Instagram URL. (The ">" check is just in case the HTML is missing the end quote for some reason.)

How to select only the characters after a repeated symbol in a long string

If i am using C# and i have a string coming in from a database like this:
\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML
And i only want this part of the string:
1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML
How can i get this string if there is more than one "\" symbol?
You can use the LastIndexOf() method of the String class:
string s = #"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA.xml";
Console.Out.WriteLine(s.Substring(s.LastIndexOf('\\') + 1));
Hope, this helps.
Use String.Split to split string by parts and then get the last part.
Using LINQ Enumerable.Last() :
text.Split('\\').Last();
or
// todo: add null-empty checks, etcs
var parts = text.Split('\\');
strign lastPart = parts[parts.Length - 1];
You can use a combination of String.LastIndexOf("\") and String.Substring(lastIndex+1). You could also use (only in the sample you provided) Path.GetFileName(theString).
string[] x= line.Split('\');
string goal =x[x.Length-1];
but linq will be easier
You can use regex or split the string by "\" symbol and take the last element of array
using System.Linq;
public class Class1
{
public Class1()
{
string s =
#"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML";
var array = s.Split('\\');
string value = array.Last();
}
}
newstring = string.Substring(string.LastIndexOf(#"\")+1);
It seems like original string is like filePath.
This could be one easy solution.
string file = #"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA.xml";
string name = System.IO.Path.GetFileName(file);

Categories

Resources