C#
  Home arrow C# arrow Page 9 - Strings and Characters, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Strings and Characters, Part 1
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    2004-07-21

    Table of Contents:
  • Strings and Characters, Part 1
  • 2.2 Determine if a character is in a Specified Range
  • 2.3 Controlling Case Sensitivity when Comparing Two Characters
  • 2.4 Finding All Occurrences of a Character Within a String
  • 2.5 Finding the Location of All Occurrences of a String Within Another String
  • 2.6 The Poor Man’s Tokenizer Problem
  • 2.7 Controlling Case Sensitivity when Comparing Two Strings
  • 2.8 Comparing a String to the Beginning or End of a Second String
  • 2.9 Inserting Text into a String
  • 2.10 Removing or Replacing Characters Within a String
  • 2.11 Encoding Binary Data as Base64
  • 2.12 Decoding a Base64-Encoded Binary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Strings and Characters, Part 1 - 2.9 Inserting Text into a String


    (Page 9 of 12 )

    Problem

    You have some text (either a char or a string value) that needs to be inserted at a specific location inside of a second string.

    Solution

    Using the Insert instance method of the string class, a string or char can easily be inserted into a string. For example, in the code fragment:

    string sourceString = "The Inserted Text is here -><-";

    sourceString = sourceString.Insert(28, "Insert-This"); Console.WriteLine(sourceString);

    the string sourceString is inserted between the > and < characters in a second string. The result is:

    The Inserted Text is here ->Insert-This<-

    Inserting the character in sourceString into a second literal string between the > and < characters is shown here:

    string sourceString = "The Inserted Text is here -><-";
    char insertChar = '1';

    sourceString = sourceString.Insert(28, Convert.ToString(insertChar)); Console.WriteLine(sourceString);

    There is no overloaded method for Insert that takes a char value, so using a string of length one is the next best solution.

    Discussion

    There are two ways of inserting strings into other strings, unless, of course, you are using the regular expression classes. The first involves using the Insert instance method on the string class. This method is also slower than the others since strings are immutable, and, therefore, a new string object must be created to hold the modified value. In this recipe, the reference to the old string object is then changed to point to the new string object. Note that the Insert method leaves the original string untouched and creates a new string object with the inserted characters.

    To add flexibility and speed to your string insertions, use the Insert instance method on the StringBuilder class. This method is overloaded to accept all of the built-in types. In addition, the StringBuilder object optimizes string insertion by not making copies of the original string; instead, the original string is modified.

    If we use the StringBuilder class instead of the string class to insert a string, our code appears as:

    StringBuilder sourceString =

    new StringBuilder("The Inserted Text is here -><-");
    sourceString.Insert (28, "Insert-This");
    Console.WriteLine(sourceString);

    The character insertion example would be changed to the following code:

    char charToInsert = '1';
    StringBuilder sourceString =

    new StringBuilder("The Inserted Text is here -><-");
    sourceString.Insert (28, charToInsert);
    Console.WriteLine(sourceString);

    Note that when using the StringBuilder class, you must also use the System.Text namespace.

    See Also

    See the “String.Insert Method” topic in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT