Flylib.com

Books Software

 
 
 

Joining a String


Joining a String

Joining a string is the opposite of splitting a string. It's done by invoking the Join function in System.String and passing an array of strings containing all the pieces you wish to join, and a string with the characters to use as a delimiter. The Join function then returns a single string by gluing all the segments from the array together and placing the delimiter between them.

To create a string by joining other strings:

  1. Type string[] pieces = new string[10] ; where pieces is a variable to store a set (array) of strings, and 10 is the number of segments that you wish to glue together.

  2. Type pieces [0] = "any string" ; where pieces is the variable declared in step 1 and [0] is the element in the array that you wish to set. You can set each element in the string array by specifying the index from zero to the number of elements minus one.

  3. Declare a string variable to hold the new string resulting from the join.

  4. Type =System.String.Join(",",pieces); where "," is the character to use as a delimiter and pieces is the array of strings ( Figure 4.53 ).

    Figure 4.53 Join does the opposite of Splitit creates one string from an array of strings and uses a delimiter to join each piece.
    string[] characters = new string[5];
    characters[0] = "Mickey";
    characters[1] = "Minnie";
    characters[2] = "Pluto";
    characters[3] = "Goofy";
    characters[4] = "Donald";
    
    string Disney =
    
    string.Join(",",characters);
    
    Response.Write(Disney);
    /*
    outputs: Mickey,Minnie,Pluto,Goofy,Donald
    */
    

graphics/tick.gif Tip

  • The delimiter string doesn't have to be a single character. It can be a series of characters, as in Figure 4.54 .

    Figure 4.54 When you put strings together with Join you aren't limited to a single character to put in between each segment.
    string[] phrases = new string[5];
    phrases[0] = "Hi";
    phrases[1] = "How are you";
    phrases[2] = "";
    phrases[3] =
    "Like we got to get together";
    phrases[4] = "ok";
    
    string collegeTalk =
    string.Join(
    
    " dude "
    
    ,phrases);
    Console.WriteLine(collegeTalk);
    
    /*
    outputs: Hi dude How are you dude dude Like we got to get together dude ok
    */
    



Uppercasing and Lowercasing

You can easily produce a version of a string where all the characters are uppercase or all the characters are lowercase.

To produce a string in either uppercase or lowercase form:

  1. Declare a new string variable.

  2. Type = str.ToUpper(); or = str.ToLower() accordingly , where str is a variable holding the original string ( Figure 4.55 ).

    Figure 4.55 The ToUpper function creates a new string object from the original where all characters are in uppercase.
    string msg= "When are you going to be done?";
    
    string screamMsg = msg.ToUpper();
    
    /*
    outputs:
    WHEN ARE YOU GOING TO BE DONE?
    */
    

graphics/tick.gif Tip

  • The ToUpper and ToLower functions don't change the original string. They produce new strings (and therefore new string buffers) with the characters' case modified.



Formatting Strings

Formatting enables you to define templates for strings. The template tells the string class the text that doesn't change. In the template you also specify placeholders for the text that does change. The string class then fills in the placeholders with text that you provide. This makes it easier for you, because the string class does the hard work of concatenating the parts of fixed text with the parts of variable text.

To format strings:

  1. Type string str where str is the name of the string that will hold the formatted text.

  2. Type =string.Format( .

  3. Type a formatting string enclosed in quotes. The formatting string tells the string class the fixed text.

  4. Within the formatting string, type {0} , {1} , {2} , etc. to set placeholders for the variable text. For example: "LastName={0} and FirstName={1}" .

  5. Type a comma.

  6. Type the name of the variable that contains the dynamic text for the string.

  7. Repeat steps 5 and 6 for each placeholder in step 4.

  8. Type ); to end the statement ( Figure 4.56 ).

    Figure 4.56 You can build a format string to serve as a template. In the format string you specify placeholders for each segment. The placeholders are specified with curly brackets.
    string firstName = "Bill";
    string lastName = "Smith";
    
    string templ =
    "Select * from contacts where ";
    templ +=
    "LastName={0} and FirstName={1}";
    
    string SQL =
    
    string.Format(templ,firstName,lastName);
    
    Response.Write(SQL);
    
    /*
    outputs:
    Select * from contacts where
    LastName=Bill and FirstName=Smith
    */
    

graphics/tick.gif Tip

  • You can use the same placeholder more than once. For example, if you were writing a form letter, you could repeat the recipient's name more than once. To accomplish that you simply use the placeholder string, for example {0} more than once ( Figure 4.57 ).

    Figure 4.57 Each placeholder has an index number. You can insert the same word in many places in the string if you repeat the index number that corresponds to the word.
    string company = "Federation";
    string customer = "Chewbacca";
    
    string templ = "Dear Mr.
    
    {1}
    
    ,<br>";
    templ += "Welcome to the
    
    {0}
    
    . ";
    templ += "The
    
    {0}
    
    is glad that you have become ";
    templ += "a warp capable species.";
    
    string letter =
    string.Format(templ,company,customer);
    Response.Write(letter);
    
    /*
    outputs:
    Dear Mr.
    
    Chewbacca
    
    ,
    
    Welcome to the
    
    Federation
    
    . The
    
    Federation
    
    is glad that you have become a warp capable species. */