I'm trying to title case some text that may contain html escape characters. Is there any way of doing this other than with regular expressions? Here's some example code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string input = "B&G fried pie";
string output = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input.ToLowerInvariant());
Console.WriteLine(output); //Should be B&G Fried Pie
Console.ReadKey();
}
}
}
Another way I can think of is to replace & with &, do my title case, then replace the & with &.
You can use the System.Web.HttpUtility class to decode and encode html strings, so your code would then look something like:
private static string ToTitleCase(string input)
{
return input == null
? null
: HttpUtility.HtmlEncode(CultureInfo.InvariantCulture.TextInfo
.ToTitleCase(HttpUtility.HtmlDecode(input.ToLowerInvariant())));
}
And in use it would look something like:
Console.WriteLine(ToTitleCase("B&G fried pie"));
Related
Given the following string:
a:3:{s:6:"status";i:1;s:3:"msg";s:39:"Transaction has been already Completed.";s:6:"result";s:1620:"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCIsImFkZGVkb24iOiIyMDE5LTA1LTE3IDE3OjEwOjEzIiwic HJvZHVjdGluZm8iOiJPZmZsaW5lIER5bmFtaWMgUVIiLCJmaXJzdG5hbWUiOiIiLCJsYXN0 bmFtZSI6IiIsImFkZHJlc3MxIjoiIiwiYWRkcmVzczIiOiIiLCJjaXR5IjoiIiwic3RhdGUiOiIiLCJjb3V udHJ5IjoiIiwiemlwY29kZSI6IiIsImVtYWlsIjoiIiwicGhvbmUiOiIiLCJ1ZGYxIjoiIiwidWRmMiI6IiIsI nVkZjMiOiIiLCJ1ZGY0IjoiIiwidWRmNSI6IiIsInVkZjYiOiIiLCJ1ZGY3IjoiIiwidWRmOCI6IiIsInVk ZjkiOi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iZmllbGQ4IjoiIiwiZmllbGQ5IjoiU1VDQ0VTU3xDb21wbGV 0ZWQgVXNpbmcgQ2FsbGJhY2siLCJwYXltZW50X3NvdXJjZSI6InBheXUiLCJQR19UWVBF IjoiQlFSIiwiZXJyb3IiOiJFMDAwIiwiZXJyb3JfTWVzc2FnZSI6Ik5vIEVycm9yIiwibmV0X2Ftb3V udF9kZWJpdCI6MCwidW5tYXBwZWRzdGF0dXMiOiJkcm9wcGVkIiwiaGFzaCI6IjgxMWI0N TE2ODdhYzE5NjYwMTk4MjVjYjgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9"
Using Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = #"\{(?'bracket'.*)\}";
string input = "a:3:{s:6:\"status\";i:1;s:3:\"msg\";s:39:\"Transaction has been already Completed.\";s:6:\"result\";s:1620:\"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCIsImFkZGVkb24iOiIyMDE5LTA1LTE3IDE3OjEwOjEzIiwic HJvZHVjdGluZm8iOiJPZ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jgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9\";}";
string insideBrackets = Regex.Match(input, pattern).Groups["bracket"].Value;
string[] messages = insideBrackets.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
string output = messages.Where(x => x.StartsWith("s:1620:")).First();
}
}
}
I am trying to load Google and get the ID of the searchbox. The ID of the box is "lst-ib". Which when the program goes to debug it is expecting a semicolon.
Is there a way around it to get the element id? So far I have:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public void Main(string[] args)
{
Process.Start("www.google.com");
HtmlElement lst-ib = WebBrowser1.Document.All["foo"];
//expects a semi colon on the line above after the element id
if (lst-ib != null)
{
lst-ib.InnerText = "test";
}
Console.ReadKey();
}
}
}
That is C# code and - is not valid in identifiers. Feel free to name the variable as you wish – it has no bearing on what the ID of the element is.
The - is an operator, you cannot use this way!
Here you will find more information about operators:
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
I recomend you rename - (trace) to _ (underline) or anyway you want
=D
Sorry for the title, I had a hard time trying to summarize this.
I'd like to replace an unknown number of instances of a string with a wrapper. So I'd like to replace this:
Test with two:\t\t tab characters
With this:
Test with two:<span class="" style="white-space:pre">\t\t</span> tab characters
There could be any number of \t characters in the given string, and in multiple locations.
The reason I'm trying to do this is our software uses Chilkat to sent emails, and if HTML content contains tabs, these are not shown at the receiving end. When sent, we use \t to represent a tab, and when viewing the source of the received email, the tabs are there, but outside the source they are not:
Outside Source:
Source:
I tested with GMail, and this wraps the tabs:
I understand this maybe be a Chilkat issue, but I can't find much help on the topic, but if I can get around it as above, I'm willing to try it.
Use this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t+)", "<span class=\"\" style=\"white-space:pre\">$1</span>");
}
}
}
This is what I ended up using. The capture group was picking up each repetition individually, and then the replace was only replacing the first found instance, not the entire capture group
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t)+", "<span class=\"\" style=\"white-space:pre\">$&</span>");
}
}
}
I'm working on a Windows application where I need to use clipboard data. I am trying to copy text from clipboard by the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MultiValuedClipBoard
{
class Class1
{
public String SwapClipboardHtmlText(String replacementHtmlText)
{
String returnHtmlText = "hello";
if (Clipboard.ContainsText(TextDataFormat.Html))
{
returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
}
return returnHtmlText;
}
}
}
Calling the above function by:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace MultiValuedClipBoard
{
class Program
{
static void Main(string[] args)
{
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.ReadLine();
}
}
}
When running this code it gives the output "Hello" which is the default value, not clipboard data.
Your code will not work because of two reasons:
[1] When you say:
if (Clipboard.ContainsText(TextDataFormat.Html))
Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. So, this if condition should not be there.
[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. So,
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
becomes
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
Hence, effectively, your new code becomes something like this:
String returnHtmlText = "hello";
//if (Clipboard.ContainsText(TextDataFormat.Html))
//{
returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
//}
return returnHtmlText;
Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false. Which means that the clipboard in fact does not contain text in the format you specify.
I changed your program to prove the point:
[STAThread]
static void Main(string[] args)
{
Clipboard.SetText("boo yah!", TextDataFormat.Html);
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
Console.ReadLine();
}
Output:
boo yah!
chetan
i have a URL like http%3A%2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG
I need to get it decode correctly. I believe that is Hexadecimal. Can't find any C# code that would do that.
thank you
From C# URLDecode turns %5C into \\\\ instead of \
using System;
using System.Web;
string url = "http%3A2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG"
string decoded = HttpUtility.UrlDecode(url);
Outside of a web application, this is a way:
using System;
using System.Net;
namespace urldecoder
{
internal class Program
{
static void Main(string[] args)
{
String EncodedString = "http%3A%2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG";
String DecodedString = WebUtility.UrlDecode(EncodedString);
Console.WriteLine(DecodedString);
Console.ReadLine();
}
}
}
See: system-net-webutility-urldecode(system-string)