Skip to content

Latest commit

 

History

History
90 lines (72 loc) · 2.2 KB

strings.md

File metadata and controls

90 lines (72 loc) · 2.2 KB

Strings

Strip non-ASCII characters from a string

Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s))

Web-Safe URL string

/// <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('-');
}

Place variables in strings with $ and {}

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

Ignore \ in strings with @

string path = @"C:\path\to\file.txt";

Note that string path1 = "C:\path\to\file.txt" won't even compile: Unrecognized escape sequence

String Formatting of Numbers

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

String contains substring

if (s.contains("substr")) {/* stuff */}

Integer to Binary string

lblBinaryByte1.Text = Convert.ToString((int)nudByte1.Value, 2).PadLeft(8, '0'); 

String to integer

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);