C# convert a string to unicode encoding - c#

I need to convert a string i.e. "hi" to & #104;& #105; is there a simple way of doing this? Here is a website that does what I need. http://unicode-table.com/en/tools/encoder/

Try this:
var s = "hi";
var ss = String.Join("", s.Select(c => "&#" + (int)c + ";"));

Try this:
string myString = "Hi there!";
string encodedString = myString.Aggregate("", (current, c) => current + string.Format("&#{0};", Convert.ToInt32(c)));

Based on the answer to this question:
static string EncodeNonAsciiCharacters(string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
string encodedValue = "&#" + ((int)c).ToString("d4"); // <------- changed
sb.Append(encodedValue);
}
return sb.ToString();
}

Related

Linq Read XML with <br> tag

I have a xml file, structure is like following:
<template><body>public DiffSectionType Type<template:br/>{<template:br/><template:tab/>get<template:br/><template:tab/>{<template:br/><template:tab/><template:tab/>return _Type;<template:br/><template:tab/>}<template:br/>}</body></template>
I would like to be more readable, like:
public DiffSectionType Type
{
get
{
return _Type;
}
}
<template:br/> => new line
<template:tab/> => tab
I can read body string, but not able to put it in correct format,
I have tried
var document = XDocument.Load("template.xml");
var body = from element in document.Elements("template").Elements("body")
select element;
foreach(var v in body)
{
Console.WriteLine(v.Value);
}
You could use Regex to solve this so something like this:
string str = #"<template><body>public DiffSectionType Type<template:br/>{<template:br/><template:tab/>get<template:br/><template:tab/>{<template:br/><template:tab/><template:tab/>return _Type;<template:br/><template:tab/>}<template:br/>}</body></template>";
str = Regex.Replace(str, "<template:br\x2F>", Environment.NewLine);
str = Regex.Replace(str, "<template:tab\x2F>", "\t");
str = Regex.Replace(str, "(<\x2Ftemplate>)|(<template>)", "");
str = Regex.Replace(str, "(<\x2Fbody>)|(<body>)", "");

Replace string after at with another string

I have two strings.
First string:
"31882757623"<sip:+31882757623#asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738
Second string:vandrielfinance.nl
I want to replace asklync.nl to vandrielfinance.nl in the first string after the # with the second string (vandrielfinance.nl). Everything else will stay the same.
So the new string will be:
"31882757623"<sip:+31882757623#vandrielfinance.nl;user=phone>;epid=5440626C04;tag=daa784a738
Here is what I have so far:
static string ReplaceSuffix(string orginal, string newString)
{
string TobeObserved = "#";
orginal = "\"31882757623\"<sip:+31882757623#asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738";
string second = "vandrielfinance.nl";
string pattern = second.Substring(0, second.LastIndexOf("#") + 1);
string code = orginal.Substring(orginal.IndexOf(TobeObserved) + TobeObserved.Length);
//newString = Regex.Replace(code,second, pattern);
newString = Regex.Replace(second, orginal, pattern);
string hallo = orginal.Replace(newString, second);
Console.Write("Original String: {0}", orginal);
Console.Write("\nReplacement String: \n{0}", newString);
Console.WriteLine("\n" + code);
return newString;
}
why not string.Replace?
string s = "\"31882757623\"<sip:+31882757623#asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738";
string t = "vandrielfinance.nl";
string u = s.Replace("asklync.nl", t);
Console.WriteLine(u);
I'm not really a fan a string.Split(), but it made for quick work in this case:
static string ReplaceSuffix(string orginal, string newString)
{
var segments = original.Split(";".ToCharArray());
var segments2 = segments[0].Split("#".ToCharArray());
segments2[1] = newString;
segments[0] = string.Join("#", segments2);
var result = string.Join(";", segments);
Console.WriteLine("Original String:\n{0}\nReplacement String:\n{1}, original, result);
return result;
}
If the original domain will really always be asklync.nl, you may even be able to just do this:
static string ReplaceSuffix(string orginal)
{
var oldDomain = "asklync.nl";
var newDomain = "vandrielfinance.nl";
var result = original.Replace(oldDomain, newDomain);
Console.WriteLine("Original String:\n{0}\nReplacement String:\n{1}, original, result);
return result;
}
This should work
var orginal = "\"31882757623\"<sip:+31882757623#asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738";
string second = "vandrielfinance.nl";
var returnValue = string.Empty;
var split = orginal.Split('#');
if (split.Length > 0)
{
var findFirstSemi = split[1].IndexOf(";");
var restOfString = split[1].Substring(findFirstSemi, split[1].Length - findFirstSemi);
returnValue = split[0] + "#" + second + restOfString;
}
Console.WriteLine("Original String:");
Console.WriteLine("{0}", orginal);
Console.WriteLine("Replacement String:");
Console.WriteLine("{0}", returnValue);
//return returnValue;
I'm not a huge fan of RegEx or string.Split, especially when a string function already exists to replace a portion of a string.
string orginal = "\"31882757623\"<sip:+31882757623#asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738";
string second = "vandrielfinance.nl";
int start = orginal .IndexOf("#");
int end = orginal .IndexOf(";", start);
string newString = orginal .Replace(orginal.Substring(start, end-start), second );
Console.WriteLine(orginal );
Console.WriteLine(newString);

Adding chars before and after each word in a string

Imagine we have a string as :
String mystring = "A,B,C,D";
I would like to add an apostrophe before and after each word in my string.Such as:
"'A','B','C','D'"
How can i achieve that?
What's your definition of a word? Anything between commas?
First get the words:
var words = mystring.Split(',');
Then add the apostrophes:
words = words.Select(w => String.Format("'{0}'", w));
And turn them back into one string:
var mynewstring = String.Join(",", words);
mystring = "'" + mystring.replace(",", "','") + "'";
I would let each "word" be determined by the regex \b word boundary. So, you have:
var output = Regex.Replace("A,B,C,D", #"(\b)", #"'$1");
string str = "a,b,c,d";
string.Format("'{0}'", str.Replace(",", "','"));
or
string str = "a,b,c,d";
StringBuilder sb = new StringBuilder(str.Length * 2 + 2);
foreach (var c in str.ToCharArray())
{
sb.AppendFormat((c == ',' ? "{0}" : "'{0}'"), c);
}
str = sb.ToString();
string mystring = "A,B,C,D";
string[] array = mystring.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string newstring = "";
foreach (var item in array)
{
newstring += "'" + item + "',";
}
newstring = newstring.Remove(newstring.Length - 1);
Console.WriteLine(newstring);
Output will be;
'A','B','C','D'
Here a DEMO.
Or more simple;
string mystring = "A,B,C,D";
Console.WriteLine(string.Format("'{0}'", mystring.Replace(",", "','")));
you can use regular expressions to solve this problem
like this:
string words= "A,B,C,D";Regex reg = new Regex(#"(\w+)");words = reg.Replace(words, match=> { return string.Format("'{0}'", match.Groups[1].Value); });

c#:String to integer conversion?

I have the string in format of Rs.2, 50,000. Now I want to read the string value as 250000 and insert into database (let assume that Rs 2,50,000 coming from third party like web services, wcf services, or from other application ).Is There any direct solution for this without using
String.Replace() can u write the code to remove the commas.
Something like this works for me
string numb="2,50,000";
int.TryParse(numb, NumberStyles.AllowThousands, CultureInfo.InvariantCulture.NumberFormat,out actualValue);
Something like this: (I haven't tested this at all)
string str = "RS.2,50,000";
str = str.substring(3, str.length);
string arr[] = str.split(",");
string newstr;
foreach (string s in arr)
{
newstr += s;
}
Try
string str = "25,000,000";
str = string.Join(string.Empty, str.Split(','));
int i = Convert.ToInt32(str);
Try this:
string rs = "Rs.2, 50,000";
string dst="";
foreach (char c in rs)
if (char.IsDigit(c)) dst += c;
rs is source string, wihle in dst you have string you need.
Something a little different:
string rs = "Rs.2, 50,000";
string s = String.Join("", Regex.Split(rs, #"[^\d]+"));

How do I use the Aggregate function to take a list of strings and output a single string separated by a space?

Here is the source code for this test:
var tags = new List<string> {"Portland", "Code","StackExcahnge" };
const string separator = " ";
tagString = tags.Aggregate(t => , separator);
Console.WriteLine(tagString);
// Expecting to see "Portland Code StackExchange"
Console.ReadKey();
Update
Here is the solution I am now using:
var tagString = string.Join(separator, tags.ToArray());
Turns out string.Join does what I need.
For that you can just use string.Join.
string result = tags.Aggregate((acc, s) => acc + separator + s);
or simply
string result = string.Join(separator, tags);
String.Join Method may be?
This is what I use
public static string Join(this IEnumerable<string> strings, string seperator)
{
return string.Join(seperator, strings.ToArray());
}
And then it looks like this
tagString = tags.Join(" ")

Categories