Skip to content

Latest commit

 

History

History
56 lines (45 loc) · 1.54 KB

strings.md

File metadata and controls

56 lines (45 loc) · 1.54 KB

Strings

Strip non-ASCII characters from a string

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

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