This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How can I simply check if a string contains {x} (x can be any number)?
I guess there is some simple RegEx to do this.
"This string contains {0} a placeholder".HasPlaceholder == true
and
"This string contains no placeholder".HasPlaceholder == false
You can write a simple extension and use a regex:
public static class StringExtensions
{
public static bool HasPlaceholder(this string s)
{
return Regex.IsMatch(s, "{\\d+}");
}
}
This regex works only for the placeholders you specified (containing only a number).
For a full placeholder you would need something like "{\\d+(,-?\\d+)?(:[A-Z]\\d*)?}". But this still needs refinement. See "Standard Numeric Format Strings" for a full list of valid symbols.
You can use this extension like this:
string s = "This string contains {0} a placeholder";
if (s.HasPlaceholder())
Console.WriteLine("Contains placeholders");
Related
This question already has answers here:
Is there an easy way to return a string repeated X number of times?
(21 answers)
Closed 2 years ago.
Is there any way to interpolate variable several times without repeating?
For example:
var name = "bla";
Console.WriteLine($"foo {name:repeat:2} bar")
to print
foo blabla bar
I'm particularly interested in interpolating several line breaks instead of repeating {Environment.NewLine} several times in the interpolation mask like this:
$"{Environment.NewLine}{Environment.NewLine}"
public static string Repeat(this string s, int times, string separator = "")
{
return string.Join(separator, Enumerable.Repeat(s, times));
}
Then use:
Console.WriteLine($"foo {name.Repeat(2)} bar")
You could write an extension method for the string type, thats repeating its input. Then simply use this method within the curly braces.
You could also use
var name = "bla";
Console.WriteLine("foo {0}{0} bar", name);
// or
var s = String.Format("foo {0}{0} bar", name);
It will help you not repeating the same string, just index of it.
More about String Format
This question already has answers here:
String interpolation C#: Documentation of colon and semicolon functionality
(2 answers)
Closed 1 year ago.
using System;
public class Example
{
public static void Main()
{
Console.WriteLine ($"{1:5d}");
}
}
// The example displays the following output:
// 5d
What does the ":" do in the interpolated string?
I know if I write,
$"{1:d5}"
I will get,
00001
but with this I'm not getting an error/warning means it has to mean something that I don't know.
FYI, I'm on C#7.
It is separator for format string. It allows you to specfy formatting relevant to the type of value on the left. In first case it tries to apply custom format, but since there is no placeholder for actual value - you get only the "value" of the format (try something like Console.WriteLine ($"{1:00000SomeStringAppended}"); for example, "5d" has the same meaning as "SomeStringAppended" in my example). The second one - d5 is a standart decimal format specifier, so you get corresponding output containing formatted value.
The following two line would be give the same results - 00001.
var i = $"{1:d5}";
var j = string.Format("{0:d5}", 1);
A colon : in curly brackets is string formatting.
You can read more about string formatting here and about string formatting in a string interpolation here
This question already has answers here:
How can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}
This question already has answers here:
Make first letter of a string upper case (with maximum performance)
(42 answers)
Closed 7 years ago.
I have already taken a look at such posts like:
Format to first letter uppercase
How to capitalise the first letter of every word in a string
But none of these seem to actually work. I would have thought to start with that there would just be a:
.Capitalize();
Like there is:
.Lower(); & .Upper();
Are there any documentation or references regarding converting to a string like the following?
string before = "INVOICE";
To then becoming:
string after = "Invoice";
I receive no errors using the way the posts solutions I read give me, however, the before still remains capitalized.
What about using ToUpper on the first char and ToLower on the remaining string?
string after = char.ToUpper(before.First()) + before.Substring(1).ToLower();
You can create a method that does something like this:
string UppercaseFirst(string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
return char.ToUpper(str[0]) + str.Substring(1).ToLower();
}
And use it like this:
string str = "thISstringLOokSHorribLE";
string upstr = UppercaseFirst(str);
to get this:
Thisstringlookshorrible
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I convert a C# string value to an escaped string literal
How can i entitize an arbitrary string to get exact human readable view similar to what the debugger does? I.e. i want all special characters entitized:
"Hello,\r\nworld"
"Hello,#D$#A$world"
"Hello,0x0D0x0Aworld"
any output like that will be fine. I'm only interested in standard function in BCL (maybe some escape routine in existing serializers or helper classes, whatever). Reverse function would be cool as well.
I do not think that there is out of the box solution for your needs.
Char.Isxxx can be used to find characters that needs special processing and custom code can replace them with info you want to see.
Code below works fine for sample but there is to many different characters in Unicode to be sure that it covers all the cases.
var s = #"Hello,
world";
var builder = new StringBuilder();
foreach (var c in s)
{
if (Char.IsLetterOrDigit(c) || Char.IsPunctuation(c))
builder.Append(c);
else
{
var newStr = string.Format("#{0}$", ((int)c).ToString("X"));
builder.Append(newStr);
}
}
Console.WriteLine(builder.ToString());
Result shown in console:
Hello,#D$#A$world
You can use a series of String.Replaces:
string Entitize(string input)
{
return input.Replace("\n", "\\n").Replace("\r", "\\r"), ... ;
}