Csharp/C Sharp/Data Types/String
Содержание
- 1 A string can control a switch statement
- 2 create some strings
- 3 Creating Strings
- 4 Decoding a Base64-encoded Binary
- 5 Demonstrate Concat()
- 6 Demonstrate Concat() 2
- 7 Demonstrate escape sequences in strings.
- 8 Demonstrate string arrays
- 9 Demonstrate verbatim literal strings
- 10 Display a string in reverse by using recursion
- 11 Display the digits of an integer using words
- 12 Extracting Substrings
- 13 From Base 64 Decode String
- 14 Illustrates the use of strings 1
- 15 Introduce string
- 16 Is Palindrome
- 17 Joining Strings
- 18 Lexical Details
- 19 Padding Strings
- 20 Removing Characters
- 21 Some string operations
- 22 string: Changing Characters
- 23 String Concatenation 2
- 24 String copy
- 25 String Copy, End With and Insert
- 26 String Interning
- 27 String Manipulation
- 28 String Manipulation Concatenate
- 29 Strings: Regular Expressions
- 30 String To Char Array
- 31 Substring demo
- 32 Trimming and padding
- 33 Trimming String Spaces
- 34 Use Substring() 1
- 35 Use Substring() 2
- 36 use the addition operator (+) to concatenate strings
- 37 use the Concat() method to concatenate strings
- 38 use the Copy() method to copy a string
- 39 use the Join() method to join strings
- 40 use the PadLeft() and PadRight() methods to align strings
- 41 use the StartsWith() and EndsWith() methods to check if a string contains a specified substring at the start and end
- 42 use the Substring() method to retrieve substrings
- 43 use the Trim(), TrimStart(), and TrimEnd() methods to trim strings
- 44 Using Strings
A string can control a switch statement
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A string can control a switch statement.
using System;
public class StringSwitch {
public static void Main() {
string[] strs = { "one", "two", "three", "two", "one" };
foreach(string s in strs) {
switch(s) {
case "one":
Console.Write(1);
break;
case "two":
Console.Write(2);
break;
case "three":
Console.Write(3);
break;
}
}
Console.WriteLine();
}
}
create some strings
using System;
class MainClass {
public static void Main() {
string myString = "To be or not to be";
string myString2 = "...\t that is the question";
string myString3 = @"\t Friends, Romans, countrymen,
lend me your ears";
// display the strings and their Length properties
Console.WriteLine("myString = " + myString);
Console.WriteLine("myString.Length = "
+ myString.Length);
Console.WriteLine("myString2 = " + myString2);
Console.WriteLine
("myString2.Length = " + myString2.Length);
Console.WriteLine("myString3 = " + myString3);
Console.WriteLine
("myString3.Length = " + myString3.Length);
}
}
Creating Strings
using System;
public class CreatingStrings
{
static void Main(string[] args)
{
char MyChar = "A";
MyChar = (char)65;
char[] MyChar2 = {"H","e","l","l","o","\0"};
char[] MyChar3 = new char[6];
MyChar3[0] = "H";
MyChar3[1] = "e";
MyChar3[2] = "l";
MyChar3[3] = "l";
MyChar3[4] = "o";
MyChar3[5] = "\0";
}
}
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));
}
}
Demonstrate Concat()
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate Concat().
using System;
public class ConcatDemo1 {
public static void Main() {
string result = String.Concat("This ", "is ", "a ",
"test ", "of ", "the ",
"String ", "class.");
Console.WriteLine("result: " + result);
}
}
Demonstrate Concat() 2
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate Concat().
using System;
public class ConcatDemo {
public static void Main() {
string result = String.Concat("hi ", 10, " ",
20.0, " ",
false, " ",
23.45M);
Console.WriteLine("result: " + result);
}
}
Demonstrate escape sequences in strings.
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate escape sequences in strings.
using System;
public class StrDemo {
public static void Main() {
Console.WriteLine("Line One\nLine Two\nLine Three");
Console.WriteLine("One\tTwo\tThree");
Console.WriteLine("Four\tFive\tSix");
// embed quotes
Console.WriteLine("\"Why?\", he asked.");
}
}
Demonstrate string arrays
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate string arrays.
using System;
public class StringArrays {
public static void Main() {
string[] str = { "This", "is", "a", "test." };
Console.WriteLine("Original array: ");
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
Console.WriteLine("\n");
// change a string
str[1] = "was";
str[3] = "test, too!";
Console.WriteLine("Modified array: ");
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
}
}
Demonstrate verbatim literal strings
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate verbatim literal strings.
using System;
public class Verbatim {
public static void Main() {
Console.WriteLine(@"This is a verbatim
string literal
that spans several lines.
");
Console.WriteLine(@"Here is some tabbed output:
1 2 3 4
5 6 7 8
");
Console.WriteLine(@"Programmers say, ""I like C#.""");
}
}
Display a string in reverse by using recursion
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Display a string in reverse by using recursion.
using System;
class RevStr {
// Display a string backwards.
public void displayRev(string str) {
if(str.Length > 0)
displayRev(str.Substring(1, str.Length-1));
else
return;
Console.Write(str[0]);
}
}
public class RevStrDemo {
public static void Main() {
string s = "this is a test";
RevStr rsOb = new RevStr();
Console.WriteLine("Original string: " + s);
Console.Write("Reversed string: ");
rsOb.displayRev(s);
Console.WriteLine();
}
}
Display the digits of an integer using words
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Display the digits of an integer using words.
using System;
public class ConvertDigitsToWords {
public static void Main() {
int num;
int nextdigit;
int numdigits;
int[] n = new int[20];
string[] digits = { "zero", "one", "two",
"three", "four", "five",
"six", "seven", "eight",
"nine" };
num = 1908;
Console.WriteLine("Number: " + num);
Console.Write("Number in words: ");
nextdigit = 0;
numdigits = 0;
/* Get individual digits and store in n.
These digits are stored in reverse order. */
do {
nextdigit = num % 10;
n[numdigits] = nextdigit;
numdigits++;
num = num / 10;
} while(num > 0);
numdigits--;
// display words
for( ; numdigits >= 0; numdigits--)
Console.Write(digits[n[numdigits]] + " ");
Console.WriteLine();
}
}
Extracting Substrings
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class ExtractingSubstrings
{
static void Main(string[] args)
{
string MyClasses = "Math 101 - Algebra";
string MySubstring = MyClasses.Substring(6);
Console.WriteLine(MySubstring);
}
}
}
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);
}
}
Illustrates the use of strings 1
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_9.cs illustrates the use of strings
*/
public class Example2_9
{
public static void Main()
{
string helloWorld = "Hello World!";
System.Console.WriteLine(helloWorld);
helloWorld = "Hello World" + " from C#!";
System.Console.WriteLine(helloWorld);
helloWorld = "Hello World" + "\n from C#!";
System.Console.WriteLine(helloWorld);
const double Pi = 3.14159;
System.Console.WriteLine("Pi = " + Pi);
}
}
Introduce string
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Introduce string.
using System;
public class StringDemo {
public static void Main() {
char[] charray = {"A", " ", "s", "t", "r", "i", "n", "g", "." };
string str1 = new string(charray);
string str2 = "Another string.";
Console.WriteLine(str1);
Console.WriteLine(str2);
}
}
Is Palindrome
using System;
using System.Text;
public class MainClass {
public static bool IsPalindrome(string s) {
int iLength, iHalfLen;
iLength = s.Length - 1;
iHalfLen = iLength / 2;
for (int i = 0; i <= iHalfLen; i++) {
if (s.Substring(i, 1) !=
s.Substring(iLength - i, 1)) {
return false;
}
}
return true;
}
static void Main(string[] args) {
string[] sa = new string[]{"level", "minim", "radar"};
foreach (string v in sa)
Console.WriteLine("{0}\t{1}",v, IsPalindrome(v));
}
}
Joining Strings
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class JoiningStrings
{
static void Main(string[] args)
{
string MyString = "Hello";
string MyString2 = "World";
string JoinedString = MyString + MyString2;
Console.WriteLine(JoinedString);
string[] A = new string[2] {
"Hello", "World"
};
string Joined = string.Join(" ", A);
Console.WriteLine(Joined);
}
}
}
Lexical Details
using System;
public class StringVerbatimStrings
{
public static void Main()
{
string s = @"
C: Hello, Miss?
O: What do you mean, "Miss"?
C: I"m Sorry, I have a cold. I wish to make a complaint.";
Console.WriteLine(s);
}
}
Padding Strings
using System;
public class PaddingStrings
{
static void Main(string[] args)
{
string MyString = "Hello World";
Console.WriteLine(MyString.PadLeft(5));
}
}
Removing Characters
using System;
public class RemovingCharacters
{
static void Main(string[] args)
{
string MyString = "Hello UnderWorld";
Console.WriteLine(MyString.Remove(7, 5));
}
}
Some string operations
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Some string operations.
using System;
public class StrOps {
public static void Main() {
string str1 =
"When it comes to .NET programming, C# is #1.";
string str2 = string.Copy(str1);
string str3 = "C# strings are powerful.";
string strUp, strLow;
int result, idx;
Console.WriteLine("str1: " + str1);
Console.WriteLine("Length of str1: " +
str1.Length);
// create upper- and lowercase versions of str1
strLow = str1.ToLower();
strUp = str1.ToUpper();
Console.WriteLine("Lowercase version of str1:\n " +
strLow);
Console.WriteLine("Uppercase version of str1:\n " +
strUp);
Console.WriteLine();
// display str1, one char at a time.
Console.WriteLine("Display str1, one char at a time.");
for(int i=0; i < str1.Length; i++)
Console.Write(str1[i]);
Console.WriteLine("\n");
// compare strings
if(str1 == str2)
Console.WriteLine("str1 == str2");
else
Console.WriteLine("str1 != str2");
if(str1 == str3)
Console.WriteLine("str1 == str3");
else
Console.WriteLine("str1 != str3");
result = str1.rupareTo(str3);
if(result == 0)
Console.WriteLine("str1 and str3 are equal");
else if(result < 0)
Console.WriteLine("str1 is less than str3");
else
Console.WriteLine("str1 is greater than str3");
Console.WriteLine();
// assign a new string to str2
str2 = "One Two Three One";
// search string
idx = str2.IndexOf("One");
Console.WriteLine("Index of first occurrence of One: " + idx);
idx = str2.LastIndexOf("One");
Console.WriteLine("Index of last occurrence of One: " + idx);
}
}
string: Changing Characters
using System;
public class ChangingCharacters
{
static void Main(string[] args)
{
string MyString = "Miami, Dolphins";
Console.WriteLine(MyString);
MyString.ToUpper();
Console.WriteLine(MyString);
MyString.ToLower();
Console.WriteLine(MyString);
}
}
String Concatenation 2
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class StringConcatenation2
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Password?");
string UserPassword = Console.ReadLine();
string Password = "Victory";
if(Password.rupareTo(UserPassword) == 0)
{
Console.WriteLine("Bad Password");
}
Console.WriteLine("Good Password!");
}
}
}
String copy
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
namespace StringManipulation
{
public class TesterStringManipulationCopy
{
public void Run()
{
string s1 = "abcd";
string s2 = "ABCD";
// the string copy method
string s5 = string.Copy(s2);
Console.WriteLine(
"s5 copied from s2: {0}", s5);
// use the overloaded operator
string s6 = s5;
Console.WriteLine("s6 = s5: {0}", s6);
}
static void Main()
{
TesterStringManipulationCopy t = new TesterStringManipulationCopy();
t.Run();
}
}
}
String Copy, End With and Insert
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
namespace StringManipulation
{
public class TesterStringCopyEndWithInsert
{
public void Run()
{
string s1 = "abcd";
string s2 = "ABCD";
string s3 = @"Liberty Associates, Inc.
provides custom .NET development,
on-site Training and Consulting";
// the string copy method
string s5 = string.Copy(s2);
Console.WriteLine(
"s5 copied from s2: {0}", s5);
// Two useful properties: the index and the length
Console.WriteLine(
"\nString s3 is {0} characters long. ",
s5.Length);
Console.WriteLine(
"The 5th character is {0}\n", s3[4]);
// test whether a string ends with a set of characters
Console.WriteLine("s3:{0}\nEnds with Training?: {1}\n",
s3,
s3.EndsWith("Training") );
Console.WriteLine(
"Ends with Consulting?: {0}",
s3.EndsWith("Consulting"));
// return the index of the substring
Console.WriteLine(
"\nThe first occurrence of Training ");
Console.WriteLine ("in s3 is {0}\n",
s3.IndexOf("Training"));
// insert the word excellent before "training"
string s10 = s3.Insert(101,"excellent ");
Console.WriteLine("s10: {0}\n",s10);
// you can combine the two as follows:
string s11 = s3.Insert(s3.IndexOf("Training"),
"excellent ");
Console.WriteLine("s11: {0}\n",s11);
}
[STAThread]
static void Main()
{
TesterStringCopyEndWithInsert t = new TesterStringCopyEndWithInsert();
t.Run();
}
}
}
String Interning
using System;
public class StringInterning
{
public static void Main()
{
string s1 = "Hello";
string s2 = "Hello";
string s3 = "Hello".Substring(0, 4) + "o";
Console.WriteLine("Str == : {0}", s1 == s2);
Console.WriteLine("Ref == : {0}", (object) s1 == (object) s2);
Console.WriteLine("Str == : {0}", s1 == s3);
Console.WriteLine("Ref == : {0}", (object) s1 == (object) s3);
}
}
String Manipulation
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
namespace StringManipulation
{
public class TesterStringManipulationCompare
{
public void Run()
{
// create some strings to work with
string s1 = "abcd";
string s2 = "ABCD";
int result; // hold the results of comparisons
// compare two strings, case sensitive
result = string.rupare(s1, s2);
Console.WriteLine(
"compare s1: {0}, s2: {1}, result: {2}\n",
s1, s2, result);
// overloaded compare, takes boolean "ignore case"
//(true = ignore case)
result = string.rupare(s1,s2, true);
Console.WriteLine("Compare insensitive. result: {0}\n",
result);
}
[STAThread]
static void Main()
{
TesterStringManipulationCompare t = new TesterStringManipulationCompare();
t.Run();
}
}
}
String Manipulation Concatenate
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
namespace StringManipulation
{
public class TesterStringManipulationConcat
{
public void Run()
{
string s1 = "abcd";
string s2 = "ABCD";
// concatenation method
string s3 = string.Concat(s1,s2);
Console.WriteLine(
"s3 concatenated from s1 and s2: {0}", s3);
// use the overloaded operator
string s4 = s1 + s2;
Console.WriteLine(
"s4 concatenated from s1 + s2: {0}", s4);
}
static void Main()
{
TesterStringManipulationConcat t = new TesterStringManipulationConcat();
t.Run();
}
}
}
Strings: Regular Expressions
using System;
using System.Text.RegularExpressions;
public class RegularExpressions
{
public static void Main()
{
string s = "Oh, I hadn"t thought of that";
Regex regex = new Regex(@" |, ");
char[] separators = new char[] {" ", ","};
foreach (string sub in regex.Split(s))
{
Console.WriteLine("Word: {0}", sub);
}
}
}
String To Char Array
using System;
public class StringToCharArray
{
public static void Main()
{
string s = "Test String";
for (int index = 0; index < s.Length; index++)
Console.WriteLine("Char: {0}", s[index]);
}
}
Substring demo
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class Substrings
{
static void Main(string[] args)
{
string[] FootballTeams = new string[3] {
"Miami, Dolphins", "Oakland, Raiders", "Seattle, Seahawks"
};
foreach (string s in FootballTeams)
{
if (s.StartsWith("Miami"))
Console.WriteLine("Awesome!");
else
Console.WriteLine("Bummer Dude!");
}
}
}
}
Trimming and padding
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Trimming and padding.
using System;
public class TrimPadDemo {
public static void Main() {
string str = "test";
Console.WriteLine("Original string: " + str);
// Pad on left with spaces.
str = str.PadLeft(10);
Console.WriteLine("|" + str + "|");
// Pad on right with spaces.
str = str.PadRight(20);
Console.WriteLine("|" + str + "|");
// Trim spaces.
str = str.Trim();
Console.WriteLine("|" + str + "|");
// Pad on left with #s.
str = str.PadLeft(10, "#");
Console.WriteLine("|" + str + "|");
// Pad on right with #s.
str = str.PadRight(20, "#");
Console.WriteLine("|" + str + "|");
// Trim #s.
str = str.Trim("#");
Console.WriteLine("|" + str + "|");
}
}
Trimming String Spaces
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_6___Strings
{
public class TrimmingSpaces
{
static void Main(string[] args)
{
string MyString = " Hello, World ! ";
MyString.TrimStart();
Console.WriteLine(MyString);
MyString.TrimEnd();
Console.WriteLine(MyString);
MyString.Trim(char.Parse("!"));
Console.WriteLine(MyString);
}
}
}
Use Substring() 1
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use Substring().
using System;
public class SubStr {
public static void Main() {
string orgstr = "C# makes strings easy.";
// construct a substring
string substr = orgstr.Substring(5, 12);
Console.WriteLine("orgstr: " + orgstr);
Console.WriteLine("substr: " + substr);
}
}
Use Substring() 2
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use Substring().
using System;
public class SubstringDemo {
public static void Main() {
string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("str: " + str);
Console.Write("str.Substring(15): ");
string substr = str.Substring(15);
Console.WriteLine(substr);
Console.Write("str.Substring(0, 15): ");
substr = str.Substring(0, 15);
Console.WriteLine(substr);
}
}
use the addition operator (+) to concatenate strings
using System;
class MainClass {
public static void Main() {
string myString6 = "To be, " + "or not to be";
Console.WriteLine("\"To be, \" + \"or not to be\" = " + myString6);
}
}
use the Concat() method to concatenate strings
using System;
class MainClass {
public static void Main() {
string myString4 = String.Concat("Friends, ", "Romans");
Console.WriteLine("String.Concat(\"Friends, \", \"Romans\") = " + myString4);
string myString5 = String.Concat("Friends, ", "Romans, ", "and countrymen");
Console.WriteLine("String.Concat(\"Friends, \", \"Romans, \", " +"\"and countrymen\") = " + myString5);
}
}
use the Copy() method to copy a string
using System;
class MainClass {
public static void Main() {
string myString4 = String.Concat("Friends, ", "Romans");
Console.WriteLine("myString4 = " + myString4);
Console.WriteLine("Copying myString4 to myString7 using Copy()");
string myString7 = String.Copy(myString4);
Console.WriteLine("myString7 = " + myString7);
}
}
use the Join() method to join strings
using System;
class MainClass {
public static void Main() {
string[] myStrings = {"To", "be", "or", "not","to", "be"};
string myString9 = String.Join(".", myStrings);
Console.WriteLine("myString9 = " + myString9);
}
}
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 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\"");
}
}
}
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 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);
}
}
Using Strings
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";
}
}