C# String Examples – String Examples C#

"I tried to learn .NET by taking boot camp classes that cost me $6,000. After taking the classes, I discovered tons of free (and very cheap) videos on the internet created by experts in the field. I found the videos to be much better than the classrooms. The videos were presented by Microsoft presenters that knew the material backwards and forwards, and they were much cheaper and better than the classroom teachers. I have spent the last three years organizing and hunting down the videos for my own use and decided that all could benefit by sharing what I found." - Wade Harvey

Click Here for Free 24 hour pass to lynda.com.

I AM VERY EXCITED ABOUT THIS AMAZING NEW DISCOVERY

Over 40,000 Top-Notch Video Tutorials (or 3,452 hours) on Lynda.com

I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.

I am planning on using Lynda Videos to help me improve in:

  1. HTML
  2. CSS
  3. Classic ASP
  4. .NET (included in ASP section)
  5. AJAX
  6. Silverlight (in Microsoft Section)
  7. SharePoint (in Microsoft Section)
  8. Web Design
  9. Blogging
  10. SEO
  11. and much more
You can learn more about Lynda.com by watching the 5-minute video at About Lynda.com. If you are only focusing on .NET, you are missing out on the "big picture" and on many of the underlying fundamentals! To be an Ideal Programmer, you need to have the largest knowledge base possible.

JavaScript tutorials



Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!

LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


DiscountASP .NET hosting JavaScript tutorials


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net

C# String Examples – String Examples C#

Purpose: – Illustrates using and in C-Sharp.

Prerequistes:

  1. Install C# (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and Pubs Databases
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build your own library of syntax examples by using same project over and over and just adding new classes to it.

Instructions:

  1. Use C# 2008 (express or standard)
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select C-Sharp for Language
    • name of project could be CSharp_Syntax.
  3. Add New folder named "LanguageBasics"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: LanguageBasics
  4. Add Class named clsString to LanguageBasics folder
    • Right-click LanguageBasics folder;
    • add new item;
    • Select Class
    • Class name could be clsString
  5. Click on copy code in code below to copy code into class clsString.cs
  6. Click on copy code in second set of code below to copy code into Program.cs
  7. Click on green arrow in toolbar in VS or press F5 to run program.

Step 1: Click on Copy Code to Cut-n-paste code into clsString.cs

  |  copy code |? 
001
002
using System;
003
using System.Text;
004
 
005
 
006
 
007
 
008
public class clsString
009
{
010
 
011
    public void Main()
012
    {
013
 
014
        //****************************************************************************************
015
        // Example #1: StartsWith(string)
016
        // Example #2: EndsWith(string)
017
        // Example #3: IndexOf(string[, startIndex][,count])
018
        // Example #4: LastIndexOf(string[, startIndex][,count])
019
        // Example #5: Insert(startIndex, string)
020
        // Example #6: PadLeft(totalWidth [, paddingCharacter])
021
        // Example #7: PadRight(totalWidth [, paddingCharacter])
022
        // Example #8: Remove(startIndex, count)
023
        // Example #9: Replace(oldString, newString)
024
        // Example #10: Substring(startIndex[, length])
025
        // Example #11: ToLower
026
        // Example #12: ToUpper
027
        // Example #13: Trim
028
        // Example #14: Length
029
        //****************************************************************************************
030
 
031
 
032
        //****************************************************************************************
033
        // Example #1: StartsWith(string)
034
        // Returns a Boolean value that indicates if the string starts with the specified string. 
035
        //****************************************************************************************
036
 
037
        Console.WriteLine("Example #1: StartsWith(string) ");
038
 
039
        string strStartsWithExample = "This is a test string";
040
 
041
        Console.WriteLine(strStartsWithExample.StartsWith("This"));
042
        //Returns True
043
 
044
        //write blank line to make output easier to read
045
        Console.WriteLine();
046
 
047
        //****************************************************************************************
048
        // Example #2: EndsWith(string)
049
        // Returns a Boolean value that indicates if the string ends with the specified string. 
050
        //****************************************************************************************
051
 
052
        Console.WriteLine("Example #2: EndsWith(string) ");
053
 
054
        string strEndsWithExample = "This is a test string";
055
 
056
        Console.WriteLine(strEndsWithExample.EndsWith("string"));
057
        //Returns True
058
 
059
        //write blank line to make output easier to read
060
        Console.WriteLine();
061
 
062
        //****************************************************************************************
063
        // Example #3: IndexOf(string[, startIndex][,count])
064
        // Returns an integer that represents the position of the first occurrence of the
065
        // specified number of characters. If you specify startIndex, that specifies
066
        // where to start looking. If you specify count, that specifies how many characters to 
067
        // to search past start. If the string is not found, this method returns -1
068
        //****************************************************************************************
069
 
070
        Console.WriteLine("Example #3: IndexOf(string[, startIndex][,count]) ");
071
 
072
        string strIndexOfExample = "This is a test string";
073
 
074
        Console.WriteLine(strIndexOfExample.IndexOf("is"));
075
        //Returns 2
076
 
077
        Console.WriteLine(strIndexOfExample.IndexOf("xyz"));
078
        //Returns -1
079
 
080
        //write blank line to make output easier to read
081
        Console.WriteLine();
082
 
083
        //****************************************************************************************
084
        // Example #4: LastIndexOf(string[, startIndex][,count])
085
        // Returns an integer that represents the position of the last occurrence of the
086
        // specified number of characters. If you specify startIndex, that specifies
087
        // where to start looking. If you specify count, that specifies how many characters to 
088
        // to search past start. If the string is not found, this method returns -1
089
        //****************************************************************************************
090
 
091
        Console.WriteLine("Example #4: LastIndexOf(string[, startIndex][,count]) ");
092
 
093
        string strLastIndexOfExample = "This is a test string";
094
 
095
        Console.WriteLine(strLastIndexOfExample.LastIndexOf("is"));
096
        //Returns 5
097
 
098
        Console.WriteLine(strLastIndexOfExample.LastIndexOf("xyz"));
099
        //Returns -1
100
 
101
        //write blank line to make output easier to read
102
        Console.WriteLine();
103
 
104
        //****************************************************************************************
105
        // Example #5: Insert(startIndex, string)
106
        // Returns a string with the specified string inserted beginning at the specified position.
107
        //****************************************************************************************
108
 
109
        Console.WriteLine("Example #5: Insert(startIndex, string) ");
110
 
111
        string strInsertExample = "This is a test string";
112
        int intIndex = 0;
113
 
114
        // First find position of word test
115
        intIndex = strInsertExample.IndexOf("test");
116
 
117
        // Insert "simple" before "test"
118
        Console.WriteLine(strInsertExample.Insert(intIndex, "simple "));
119
 
120
        //write blank line to make output easier to read
121
        Console.WriteLine();
122
 
123
        //****************************************************************************************
124
        // Example #6: PadLeft(totalWidth [, paddingCharacter])
125
        // Returns a string that is right-aligned and padded on left with character specified.
126
        // If no padddingCharacter is specified, space is used. totalWidth is total width of column
127
        // This allows you to create a nice straight column even though lengths of text vary.
128
        //****************************************************************************************
129
 
130
        Console.WriteLine("Example #6: PadLeft(totalWidth [, paddingCharacter]) ");
131
 
132
        string strPadLeftExampleA = "This is a test string";
133
        string strPadLeftExampleB = "short";
134
        string strPadLeftExampleC = "longer";
135
 
136
 
137
 
138
        // PadLeft "simple" before "test"
139
        Console.WriteLine(strPadLeftExampleA.PadLeft(35));
140
        Console.WriteLine(strPadLeftExampleB.PadLeft(35));
141
        Console.WriteLine(strPadLeftExampleC.PadLeft(35));
142
 
143
        //write blank line to make output easier to read
144
        Console.WriteLine();
145
 
146
        //****************************************************************************************
147
        // Example #7: PadRight(totalWidth [, paddingCharacter])
148
        // Returns a string that is left-aligned and padded on right with character specified.
149
        // If no padddingCharacter is specified, space is used. totalWidth is total width of column
150
        // This allows you to create a nice straight column even though lengths of text vary.
151
        //****************************************************************************************
152
 
153
        Console.WriteLine("Example #7: PadRight(totalWidth [, paddingCharacter]) ");
154
 
155
        string strPadRightExampleA = "This is a test string";
156
        string strPadRightExampleB = "short";
157
        string strPadRightExampleC = "longer";
158
 
159
 
160
 
161
        // PadRight "simple" before "test"
162
        Console.WriteLine(strPadRightExampleA.PadRight(35) + "next column starts here");
163
        Console.WriteLine(strPadRightExampleB.PadRight(35) + "next column starts here");
164
        Console.WriteLine(strPadRightExampleC.PadRight(35) + "next column starts here");
165
 
166
        //write blank line to make output easier to read
167
        Console.WriteLine();
168
 
169
        //****************************************************************************************
170
        // Example #8: Remove(startIndex, count)
171
        // Returns a string with the specified number of characters removed starting at startIndex.
172
        // If no Count is specified, the remainder of string is removed
173
        //****************************************************************************************
174
 
175
        Console.WriteLine("Example #8: Remove(startIndex, count) ");
176
 
177
        string strRemoveExample = "This is a test string";
178
        int intIndexRemove = 0;
179
 
180
        // First find position of word test
181
        intIndexRemove = strRemoveExample.IndexOf("test");
182
 
183
        // Remove rest of string starting with word "test"
184
        Console.WriteLine(strRemoveExample.Remove(intIndex));
185
 
186
        //write blank line to make output easier to read
187
        Console.WriteLine();
188
 
189
        //****************************************************************************************
190
        // Example #9: Replace(oldString, newString)
191
        // Returns a string with all occurences of oldString replaced by newString
192
        //****************************************************************************************
193
 
194
        Console.WriteLine("Example #9: Replace(oldString, newString) ");
195
 
196
        string strReplaceExample = "This is a test string";
197
 
198
        // Replace "This" with "That"
199
        Console.WriteLine(strReplaceExample.Replace("This", "That"));
200
 
201
        //write blank line to make output easier to read
202
        Console.WriteLine();
203
 
204
        //****************************************************************************************
205
        // Example #10: Substring(startIndex[, length])
206
        // Returns the string that starts at the specified position and has the specified
207
        // length. If the length is not specified, all of the characters to the end are returned.
208
        //****************************************************************************************
209
 
210
        Console.WriteLine("Example #10: Substring(startIndex[, length]) ");
211
 
212
        string strSubstringExample = "This is a test string";
213
        int intIndexSubstring = 0;
214
 
215
        // First find position of word test
216
        intIndexSubstring = strRemoveExample.IndexOf("test");
217
 
218
        // Get everything starting at "test" to end of string
219
        Console.WriteLine(strSubstringExample.Substring(intIndexSubstring));
220
 
221
        //write blank line to make output easier to read
222
        Console.WriteLine();
223
 
224
        //****************************************************************************************
225
        // Example #11: ToLower
226
        // Returns the string in Lowercase
227
        //****************************************************************************************
228
 
229
        Console.WriteLine("Example #11: ToLower");
230
 
231
        string strToLowerExample = "This is a test string";
232
 
233
        // Return Lowercase
234
        Console.WriteLine(strToLowerExample.ToLower());
235
 
236
        //write blank line to make output easier to read
237
        Console.WriteLine();
238
 
239
        //****************************************************************************************
240
        // Example #12: ToUpper
241
        // Returns the string in Uppercase
242
        //****************************************************************************************
243
 
244
        Console.WriteLine("Example #12: ToUpper ");
245
 
246
        string strToUpperExample = "This is a test string";
247
 
248
        // Return Uppercase
249
        Console.WriteLine(strToUpperExample.ToUpper());
250
 
251
        //write blank line to make output easier to read
252
        Console.WriteLine();
253
 
254
        //****************************************************************************************
255
        // Example #13: Trim
256
        // Returns the string with leading and trailing spaces removed
257
        //****************************************************************************************
258
 
259
        Console.WriteLine("Example #13: Trim ");
260
 
261
        string strTrimExample = "     ABC     ";
262
 
263
        // Return Uppercase
264
        Console.WriteLine(strTrimExample.Trim().Length);
265
 
266
        //write blank line to make output easier to read
267
        Console.WriteLine();
268
 
269
        //****************************************************************************************
270
        // Example #14: Length
271
        // Returns the string with leading and trailing spaces removed
272
        //****************************************************************************************
273
 
274
        Console.WriteLine("Example #14: Length ");
275
 
276
        string strLengthExample = "     ABC     ";
277
 
278
        // Return Uppercase
279
        Console.WriteLine(strLengthExample.Length);
280
 
281
        //write blank line to make output easier to read
282
        Console.WriteLine();
283
 
284
 
285
 
286
        //Prevent console from closing before you press enter
287
        Console.ReadLine();
288
 
289
    }
290
 
291
 
292
}
293
 
294
 
295
 
296
 
297


Step 2: Click on Copy Code to Cut-n-paste code into Program.cs
  |  copy code |? 
01
02
using System;
03
using System.Collections.Generic;
04
using System.Linq;
05
using System.Text;
06
 
07
namespace CSharp_Syntax
08
{
09
    class Program
10
    {
11
        static void Main(string[] args)
12
        {
13
            //LanguageBasics
14
 
15
 
16
            clsString myString = new clsString();
17
            myString.Main();
18
 
19
 
20
        }
21
    }
22
}
23
 
24
 
25
 
26


Step 3: Click on green arrow in toolbar in VS or press F5 to run program.

Related posts:

  1. VB.NET String – String VB.NET VB.NET String - String VB.NET ...
  2. C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
  3. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  4. C# ASP.NET String Examples – String Examples ASP.NET C# C# ASP.NET String Examples - String Examples ASP.NET C#...
  5. VB.NET ASP.NET String Examples – String Examples ASP.NET VB.NET VB.NET ASP.NET String Examples - String Examples ASP.NET VB.NET...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

This blog uses the cross-linker plugin developed by Web-Developers.Net