Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s))
/// <summary>
/// Return the input string modified to contain only numbers, lowercase letters, hyphens, and underscores.
/// </summary>
private string WebSafe(string text)
{
char[] chars = text.ToLowerInvariant()
.ToCharArray()
.Select(c => (char.IsLetterOrDigit(c) || c == '_') ? c : '-')
.ToArray();
string safe = new(chars);
while (safe.Contains("--"))
safe = safe.Replace("--", "-");
return safe.Trim('-');
}
string message = "awesome";
int thoughts = 123;
System.Console.WriteLine($"Scott is {message} and has {Math.Pow(thoughts, 2)} thoughts");
OUTPUT: Scott is awesome and has 15129 thoughts
string path = @"C:\path\to\file.txt";
Note that string path1 = "C:\path\to\file.txt"
won't even compile: Unrecognized escape sequence
- http://www.csharp-examples.net/string-format-double/
- http://www.daveoncsharp.com/2009/09/formatting-decimals-in-csharp/
- https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
msg += string.Format("Axis render time: {0:0.000} ms\n", time_redraw_axis_ms);
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
String.Format("{0:n}", 1234); // 1,234.00
string.Format("{0:n0}", 9876); // 9,876
// or this
i.ToString("0000"); - pad with zeros
i.ToString("D4"); - pad with zeros
// or double up
string.Format(String.Format("{0:0000000000.0000}", 123)); // 0000000123.0000
if (s.contains("substr")) {/* stuff */}
lblBinaryByte1.Text = Convert.ToString((int)nudByte1.Value, 2).PadLeft(8, '0');
If you are sure it will parse:
int.Parse(string)
If you are not sure it will parse:
int i;
bool success = int.TryParse(string, out i);