Interpolated string in database - c#

Following interpolated string formation works well:
string BG="7263-2323";
string PG="2983-2323";
string interpolatedString = $"{BG};{PG}";
Console.WriteLine(interpolatedString);
Result in variable interpolatedString:
7263-2323;2983-2323
But, the problem is that I store interpolatedString in database, then it does not display values for BG and PG... Instead it displays string as it is:
$\"{BG};{PG}\"
How can I solve this issue? Any idea?

You can't do that. Interpolated strings are effectively just syntactic sugar for using string.Format. Instead you would need to store your strings like this:
Hello {0}, welcome to my app
And then use string.Format:
var format = "Hello {0}, welcome to my app";
var output = string.format(format, "Bob");
Alternatively you could roll your own, for example:
var format = "Hello {name}, welcome to my app";
var output = format.Replace("{name}", "Bob");
Note that my example here isn't particularly efficient so you may want to use something like StringBuilder if you're doing a lot of this.

Related

C# String "pattern" in variable

how do I put string "pattern" to a variable.
To clearify what I want:
string word = "George";
string pattern1 = $"I met person {word}";
string pattern2 = $"I love {word}";
Console.WriteLine(pattern1);
Console.WriteLine(pattern2);
word = "Jimmy";
Console.WriteLine(pattern1);
Console.WriteLine(pattern2);
If the "pattern" system worked as I want, I would get this output:
I met person George.
I love George.
I met person Jimmy.
I love Jimmy.
You get what I want I don't want to execute the $"..." string I want to save it as "pattern" (I didn't find better word for it) and then execute it when I want (etc with changed variables).
Use the overload of Console.WriteLine that takes a composite formatting string and arguments for the string. (Note the lack of $ in front of the pattern strings - we are not performing the interpolation there, but deferring it to be done as part of the Console.WriteLine call.)
string word = "George";
string pattern1 = "I met person {0}";
string pattern2 = "I love {0}";
Console.WriteLine(pattern1, word);
Console.WriteLine(pattern2, word);
word = "Jimmy";
Console.WriteLine(pattern1, word);
Console.WriteLine(pattern2, word);
If you want to store the string after the substitution instead of writing directly to the console, use string.Format.
Just make it into a method:
private static string pattern1(string word) => $"I met person {word}";
Inside your original method, call the new method like this:
Console.WriteLine(pattern1(word));

C# How to treat a string variable as interpolated string?

the interpolated string is easy, just a string lead with $ sign. But what if the string template is coming from outside of your code. For example assume you have a XML file containing following line:
<filePath from="C:\data\settle{date}.csv" to="D:\data\settle{date}.csv"/>
Then you can use LINQ to XML read the content of the attributes in.
//assume the ele is the node <filePath></filePath>
string pathFrom = ele.Attribute("from").value;
string pathTo = ele.Attibute("to").value;
string date = DateTime.Today.ToString("MMddyyyy");
Now how can I inject the date into the pathFrom variable and pathTo variable?
If I have the control of the string itself, things are easy. I can just do var xxx=$"C:\data\settle{date}.csv";But now, what I have is only the variable that I know contains the placeholder date
String interpolation is a compiler feature, so it cannot be used at runtime. This should be clear from the fact that the names of the variables in the scope will in general not be availabe at runtime.
So you will have to roll your own replacement mechanism. It depends on your exact requirements what is best here.
If you only have one (or very few replacements), just do
output = input.Replace("{date}", date);
If the possible replacements are a long list, it might be better to use
output = Regex.Replace(input, #"\{\w+?\}",
match => GetValue(match.Value));
with
string GetValue(string variable)
{
switch (variable)
{
case "{date}":
return DateTime.Today.ToString("MMddyyyy");
default:
return "";
}
}
If you can get an IDictionary<string, string> mapping variable names to values you may simplify this to
output = Regex.Replace(input, #"\{\w+?\}",
match => replacements[match.Value.Substring(1, match.Value.Length-2)]);
You can't directly; the compiler turns your:
string world = "world";
var hw = $"Hello {world}"
Into something like:
string world = "world";
var hw = string.Format("Hello {0}", world);
(It chooses concat, format or formattablestring depending on the situation)
You could engage in a similar process yourself, by replacing "{date" with "{0" and putting the date as the second argument to a string format, etc.
SOLUTION 1:
If you have the ability to change something on xml template change {date} to {0}.
<filePath from="C:\data\settle{0}.csv" to="D:\data\settle{0}.csv" />
Then you can set the value of that like this.
var elementString = string.Format(element.ToString(), DateTime.Now.ToString("MMddyyyy"));
Output: <filePath from="C:\data\settle08092020.csv" to="D:\data\settle08092020.csv" />
SOLUTION 2:
If you can't change the xml template, then this might be my personal course to go.
<filePath from="C:\data\settle{date}.csv" to="D:\data\settle{date}.csv" />
Set the placeholder like this.
element.Attribute("to").Value = element.Attribute("to").Value.Replace("{date}", DateTime.Now.ToString("MMddyyyy"));
element.Attribute("from").Value = element.Attribute("from").Value.Replace("{date}", DateTime.Now.ToString("MMddyyyy"));
Output: <filePath from="C:\data\settle08092020.csv" to="D:\data\settle08092020.csv" />
I hope it helps. Kind regards.
If you treat your original string as a user-input string (or anything that is not processed by the compiler to replace the placeholder, then the question is simple - just use String.Replace() to replace the placehoder {date}, with the value of the date as you wish. Now the followup question is: are you sure that the compiler is not substituting it during compile time, and leaving it untouched for handling at the runtime?
String interpolation allows the developer to combine variables and text to form a string.
Example
Two int variables are created: foo and bar.
int foo = 34;
int bar = 42;
string resultString = $"The foo is {foo}, and the bar is {bar}.";
Console.WriteLine(resultString);
Output:
The foo is 34, and the bar is 42.

How to get the string data which is outside of parenthesis in C#

I need to get the data which is outside of parenthesis
string data = "English(Language)";
string result= "English";
The result should display the text "English".
I tried with Regex but not able to get the desired result.
Easiest solution that I can think of:
string data = "English(Language)";
string result = data.Substring(0, data.IndexOf('('));
That is of course, if you never need the data within the parenthesis.
Another way to do it is by using String.Split:
string data = "English(Language)";
string result = data.Split('(')[0];
This is marginally slower than the first example since it needs to allocate memory for an array.
The third way to do it is via regular-expressions:
string data = "English(Language)";
var pattern = new Regex("(\\w+\\s?)\\((\\w+)\\)", RegexOptions.Compiled);
string result = pattern.Match(data).Groups[1].Value;
This is the slowest of all the examples, but captures both "English" and "Language". It also allows for whitespace \s? between English and (Language).
A great tool for testing regular expressions is RegexPal, just remember to escape everything when you move it over to C#.
Here is a fiddle, testing the performance of all options.
Try:
string input = "English(Language)";
string regex = "(\\(.*\\))";
string output = Regex.Replace(input, regex, "");
You will need that:
using System.Text.RegularExpressions;
If you dont bother to use Regex, the below solution works fine.
string data = "English(Language)";
string result = Regex.Match(data, #"(.*)\(.*\)").Groups[1].Value;
Console.WriteLine(result); // English
Hi take a look at the Split methods:
string data = "English(Language)";
string result= "English";
var value = data.Split('(').First();
Console.WriteLine (value);
Result :
English
xd or just:
string data = "English(Language)";
string result = data.Replace("(Language)", "");

String Interpolation with format variable

I can do this:
var log = string.Format("URL: {0}", url);
or even like this
var format = "URL: {0}";
...
var log = string.Format(format, url);
I have a format defined somewhere else and use the format variable, not inline string.
In C# 6, this is seems impossible:
var format = $"URL: {url}"; // Error url does not exist
...
var url = "http://google.com";
...
var log = $format; // The way to evaluate string interpolation here
Is there anyway to use string interpolation with variable declared earlier?
C# 6 seems interpolate the string inline during compile time. However consider using this feature for localization, define a format in config or simply having a format const in a class.
No, you can't use string interpolation with something other than a string literal as the compiler creates a "regular" format string even when you use string interpolation.
Because this:
string name = "bar";
string result = $"{name}";
is compiled into this:
string name = "bar";
string result = string.Format("{0}", name);
the string in runtime must be a "regular" format string and not the string interpolation equivalent.
You can use the plain old String.Format instead.
One approach to work around that would be to use a lambda containing the interpolated string. Something like:
Func<string, string> formatter = url => $"URL: {url}";
...
var googleUrl = "http://google.com";
...
var log = formatter(googleUrl);
In C# 7.0, you could use a local function instead of a lambda, to make the code slightly simpler and more efficient:
string formatter(string url) => $"URL: {url}";
...
var googleUrl = "http://google.com";
...
var log = formatter(googleUrl);
String interpolation is not library, but a compiler feature starting with C# 6.
The holes are not names, but expressions:
var r = new Rectangle(5, 4);
var s = $"Area: {r.Width * r.Heigh}":
How would you do that for localization, as you intend to?
Even r only exists at compile time. In IL it's just a position on the method's variable stack.
I've done what you intend to do for resources and configuration files.
Since you can only have a finite set of "variables" to substitute, what I did was have an array (or dictionary, if you prefer) and use a regular expression to replace the names in the holes with its index. What I did even allowed for format specifiers.
This is supposed to be a comment to the answer from i3arnon but I do not have the reputation :-( :
But for those who come to this old thread, in string.Format the format can be a variable:
string name = "bar";
string format = "{0}";
string result = string.Format(format, name);
works.
More of an idea as opposed to an answer.
For the example shown in the question, you can do the following.
var format = "URL: ";
...
var url = "http://google.com";
...
var result= $"{format} {url}";
I have an actual project where I have to do something like this a lot:
var label = "Some Label";
var value = "SomeValue";
//both label & value are results of some logic
var result = $"{label}: {value}";
You can with the right Nuget package: https://www.nuget.org/packages/InterpolatedStringFormatter
var mystring = "a thing(and something {other})";
Console.WriteLine(mystring.Interpolate("else"));
Outputs:
a thing(and something else)
It seems that you can do it like this:
var googleUrl = "http://google.com";
var url = $"URL: {googleUrl}";
System.Console.WriteLine(url);
You can check for more details in https://msdn.microsoft.com/en-us/library/dn961160.aspx

Is it possible to include a C# variable in a string variable without using a concatenator?

Does .NET 3.5 C# allow us to include a variable within a string variable without having to use the + concatenator (or string.Format(), for that matter).
For example (In the pseudo, I'm using a $ symbol to specify the variable):
DateTime d = DateTime.Now;
string s = "The date is $d";
Console.WriteLine(s);
Output:
The date is 4/12/2011 11:56:39 AM
Edit
Due to the handful of responses that suggested string.Format(), I can only assume that my original post wasn't clear when I mentioned "...(or string.Format(), for that matter)". To be clear, I'm well aware of the string.Format() method. However, in my specific project that I'm working on, string.Format() doesn't help me (it's actually worse than the + concatenator).
Also, I'm inferring that most/all of you are wondering what the motive behind my question is (I suppose I'd feel the same way if I read my question as is).
If you are one of the curious, here's the short of it:
I'm creating a web app running on a Windows CE device. Due to how the web server works, I create the entire web page content (css, js, html, etc) within a string variable. For example, my .cs managed code might have something like this:
string GetPageData()
{
string title = "Hello";
DateTime date = DateTime.Now;
string html = #"
<!DOCTYPE html PUBLIC ...>
<html>
<head>
<title>$title</title>
</head>
<body>
<div>Hello StackO</div>
<div>The date is $date</div>
</body>
</html>
";
}
As you can see, having the ability to specify a variable without the need to concatenate, makes things a bit easier - especially when the content increases in size.
No, unfortunately C# is not PHP.
On the bright side though, C# is not PHP.
Almost, with a small extension method.
static class StringExtensions
{
public static string PHPIt<T>(this string s, T values, string prefix = "$")
{
var sb = new StringBuilder(s);
foreach(var p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
sb = sb.Replace(prefix + p.Name, p.GetValue(values, null).ToString());
}
return sb.ToString();
}
}
And now we can write:
string foo = "Bar";
int cool = 2;
var result = "This is a string $foo with $cool variables"
.PHPIt(new {
foo,
cool
});
//result == "This is a string Bar with 2 variables"
No, it doesn't. There are ways around this, but they defeat the purpose. Easiest thing for your example is
Console.WriteLine("The date is {0}", DateTime.Now);
string output = "the date is $d and time is $t";
output = output.Replace("$t", t).Replace("$d", d); //and so on
Based on the great answer of #JesperPalm I found another interesting solution which let's you use a similar syntax like in the normal string.Format method:
public static class StringExtensions
{
public static string Replace<T>(this string text, T values)
{
var sb = new StringBuilder(text);
var properties = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.ToArray();
var args = properties
.Select(p => p.GetValue(values, null))
.ToArray();
for (var i = 0; i < properties.Length; i++)
{
var oldValue = string.Format("{{{0}", properties[i].Name);
var newValue = string.Format("{{{0}", i);
sb.Replace(oldValue, newValue);
}
var format = sb.ToString();
return string.Format(format, args);
}
}
This gives you the possibility to add the usual formatting:
var hello = "Good morning";
var world = "Mr. Doe";
var s = "{hello} {world}! It is {Now:HH:mm}."
.Replace(new { hello, world, DateTime.Now });
Console.WriteLine(s); // -> Good morning Mr. Doe! It is 13:54.
The short and simple answer is: No!
string.Format("The date is {0}", DateTime.Now.ToString())
No, But you can create an extension method on the string instance to make the typing shorter.
string s = "The date is {0}".Format(d);
string.Format (and similar formatting functions such as StringBuilder.AppendFormat) are the best way to do this in terms of flexibility, coding practice, and (usually) performance:
string s = string.Format("The date is {0}", d);
You can also specify the display format of your DateTime, as well as inserting more than one object into the string. Check out MSDN's page on the string.Format method.
Certain types also have overloads to their ToString methods which allow you to specify a format string. You could also create an extension method for string that allows you to specify a format and/or parse syntax like this.
How about using the T4 templating engine?
http://visualstudiomagazine.com/articles/2009/05/01/visual-studios-t4-code-generation.aspx
If you are just trying to avoid concatenation of immutable strings, what you're looking for is StringBuilder.
Usage:
string parameterName = "Example";
int parameterValue = 1;
Stringbuilder builder = new StringBuilder();
builder.Append("The output parameter ");
builder.Append(parameterName);
builder.Append("'s value is ");
builder.Append(parameterValue.ToString());
string totalExample = builder.ToString();
Since C# 6.0 you can write string "The title is \{title}" which does exactly what you need.
you can use something like this as mentioned in C# documentation.
string interpolation
string name = "Horace";
int age = 34;
Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{");
Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");
Or combined:
Console.WriteLine("The date is {0}", DateTime.Now);
Extra info (in response to BrandonZeider):
Yep, it is kind-a important for people to realize that string conversion is automatically done. Manually adding ToString is broken, e.g.:
string value = null;
Console.WriteLine("The value is '{0}'", value); // OK
Console.WriteLine("The value is '{0}'", value.ToString()); // FAILURE
Also, this becomes a lot less trivial once you realize that the stringification is not equivalent to using .ToString(). You can have format specifiers, and even custom format format providers... It is interesting enough to teach people to leverage String.Format instead of doing it manually.

Categories