using System; public class UsingStrings { static void Main(string[] args) { string MyString = "Hello World"; string Path = @"c:Program Files"; string Path2 = "c:Program Files"; string Name = "Joe"; } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
From Base 64 Decode String
using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{ static void Main(string[] args){ foreach (byte b in Base64DecodeString("AAAA")) Console.WriteLine(b); } public static byte[] Base64DecodeString(string inputStr) { byte[] encodedByteArray = Convert.FromBase64CharArray(inputStr.ToCharArray(), 0, inputStr.Length); return (encodedByteArray); } }
Decoding a Base64-encoded Binary
using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{ static void Main(string[] args){ Console.WriteLine(Base64EncodeBytes(new byte[5] {45,34,23,54,38})); } public static string Base64EncodeBytes(byte[] inputBytes) { // Each 3 byte sequence in inputBytes must be converted to a 4 byte sequence long arrLength = (long)(4.0d * inputBytes.Length / 3.0d); if ((arrLength % 4) != 0) { // increment the array lenght to the next multiple of 4 if it is not already divisible by 4 arrLength += 4 - (arrLength % 4); } char[] encodedCharArray = new char[arrLength]; Convert.ToBase64CharArray(inputBytes, 0, inputBytes.Length, encodedCharArray, 0); return (new string(encodedCharArray)); } }
use the PadLeft() and PadRight() methods to align strings
using System; class MainClass { public static void Main() { string[] myStrings = {"To", "be", "or", "not","to", "be"}; string myString = String.Join(".", myStrings); string myString14 = '(' + myString.PadLeft(20) + ')'; Console.WriteLine("'(' + myString.PadLeft(20)+ ')' = " + myString14); string myString15 = '(' + myString.PadLeft(20, '.') + ')'; Console.WriteLine("'(' + myString.PadLeft(20, '.') =" + myString15); string myString16 = '(' + myString.PadRight(20) + ')'; Console.WriteLine("'(' + myString.PadRight(20) + ')' =" + myString16); string myString17 = '(' + myString.PadRight(20, '.') + ')'; Console.WriteLine("'(' +myString.PadRight(20, '.') + ')' = " + myString17); } }
use the Trim(), TrimStart(), and TrimEnd() methods to trim strings
using System; class MainClass { public static void Main() { string myString18 = '(' +" Whitespace ".Trim() + ')'; Console.WriteLine("'(' +" Whitespace ".Trim() + ')' = " + myString18); string myString19 = '(' + " Whitespace ".TrimStart() + ')'; Console.WriteLine("'(' +" Whitespace ".TrimStart() + ')' = " + myString19); string myString20 = '(' + " Whitespace ".TrimEnd() + ')'; Console.WriteLine("'(' +" Whitespace ".TrimEnd() + ')' = " + myString20); } }
use the Substring() method to retrieve substrings
using System; class MainClass { public static void Main() { string[] myStrings = {"To", "be", "or", "not","to", "be"}; string myString = String.Join(".", myStrings); string myString21 = myString.Substring(3); Console.WriteLine("myString.Substring(3) = " + myString21); string myString22 = myString.Substring(3, 2); Console.WriteLine("myString.Substring(3, 2) = " + myString22); string myString23 = myString.ToUpper(); Console.WriteLine("myString.ToUpper() = " + myString23); string myString24 = myString.ToLower(); Console.WriteLine("myString.ToLower() = " + myString24); } }
use the StartsWith() and EndsWith() methods to check if a string contains a specified substring at the start and end
using System; class MainClass { public static void Main() { string[] myStrings = {"To", "be", "or", "not","to", "be"}; string myString = String.Join(".", myStrings); Console.WriteLine("myString = " + myString); if (myString.StartsWith("To")) { Console.WriteLine("myString starts with "To""); } if (myString.EndsWith("be")) { Console.WriteLine("myString ends with "be""); } } }