Library code snippets

String Manipulation in C#

Trim Function

The trim function has three variations Trim, TrimStart and TrimEnd. The first example show how to use the Trim(). It strips all white spaces from both the start and end of the string.

//STRIPS WHITE SPACES FROM BOTH START + FINSIHE
string Name = " String Manipulation " ;
string NewName = Name.Trim();
//ADD BRACKET SO YOU CAN SEE TRIM HAS WORKED
MessageBox.Show("["+ NewName + "]");

TrimEnd

TrimEnd works in much the same way but u are stripping characters which you specify from the end of the string, the below example first strips the space then the n so the output is String Manipulatio.

//STRIPS CHRS FROM THE END OF THE STRING
string Name = " String Manipulation " ;
//SET OUT CHRS TO STRIP FROM END
char[] MyChar = {' ','n'};
string NewName = Name.TrimEnd(MyChar);
//ADD BRACKET SO YOU CAN SEE TRIM HAS WORKED
MessageBox.Show("["+ NewName + "]");

TrimStart

TrimStart is the same as TrimEnd apart from it does it to the start of the string.

//STRIPS CHRS FROM THE START OF THE STRING
string Name = " String Manipulation " ;
//SET OUT CHRS TO STRIP FROM END
char[] MyChar = {' ','S'};
string NewName = Name.TrimStart(MyChar);
//ADD BRACKET SO YOU CAN SEE TRIM HAS WORKED
MessageBox.Show("["+ NewName + "]");

Find String within string

This code shows how to search within a string for a sub string and either returns an index position of the start or a -1 which indicates the string has not been found.

string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);
//SHOWS START POSITION OF STRING
MessageBox.Show("Found at : " + FirstChr );

Replace string in string

Below is an example of replace a string within a string. It replaces the word Manipulatin with the correct spelling of Manipulation.

string MainString "String Manipulatin";
string CorrectString = MainString.Replace("Manipulatin", "Manipulation");
//SHOW CORRECT STRING
MessageBox.Show("Correct string is : " + CorrectString);

Strip specified number of characters from string

This example show how you can strip a number of characters from a specified starting point within the string. The first number is the starting point in the string and the second is the amount of chrs to strip.

string MainString = "S1111tring Manipulation";
string NewString = MainString.Remove(1,4);

//SHOW OUTPUT
MessageBox.Show(NewSring);

Split string with dilemeter

The example below shows how to split the string into seperate parts via a specified dilemeter. The results get put into the Split array and called back via Split[0].

string MainString = "String Manipulation";
string [] Split = MainString.Split(new Char [] {' '});
//SHOW RESULT
MessageBox.Show(Convert.ToString(Split[0]));
MessageBox.Show(Convert.ToString(Split[1]));

Comments

  1. 09 Dec 2008 at 08:58
    Eish, I dnt think my question will b clear enough, it didn't display de way I expected.
  2. 09 Dec 2008 at 08:50
    Hi, I have a question 1|Branch One 1\1|Cluster One 1\1\1|Directorate 1-A 1\1\2|Directorate 1-B 1\1\3|Directorate 1-C 1\2|Cluster Two 1\2\1|Directorate 2-A 1\2\2|Directorate 2-B 1\2\3|Directorate 2-C 1\3|Cluster Three 1)how do I read data before | delimiter from a text file into a textbox. 2) Determine how many nodes there are in the file plan number e.g Num Nodes 1 1 1\1 2 1\1\1 3 1\1\2 3 1\1\3 3 1\2 2 1\2\1 3 1\2\2 3
  3. 27 Jul 2007 at 22:10

    Very useful thread.

    I have a question.How will i do this pgm accepts a string from the user and outputs a string with the characters in reverse order.

    Thanks.

     

  4. 06 Jul 2007 at 08:42
    Nice and very good Example
  5. 12 Sep 2006 at 20:22

    OK. My bad, you do need to assign the expression to something, eg
    string cutString = originalString.Remove(1,3).


  6. 12 Sep 2006 at 19:34

    Nice explanation, just on the spot like I like them.

    Anyhow, for some reason the Remove method (as well as the Replace) don't work at all for me (as if they are not implemented at all) while the IndexOf method does work. To make my point:

    string var = "This is an example an example";
    tb1.Text = var;
    tb1.Text += var.Length;
    var.Remove(1,11);
    var.Remove(3, 9);
    tb1.Text += var;
    tb1.Text += var.Length;

    where tb1 is a text box displays:
    This is an example an example29This is an example an example29

    I dont know if I'm writing it wrong. I dont think I am. Anyways I'm new to this anyhow and some help and additional ideas would be appreciated. Thanx.
















  7. 06 Oct 2004 at 15:56
    and easy to understand. Thank you.
  8. 24 Sep 2004 at 03:00

    Excellent because of its concise yet easily understandable content.

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of String Manipulation in C#.

Leave a comment

Sign in or Join us (it's free).

Colin Harman MACITP I am 26 and live in sydney, australia. I enjoy drinking and going out, but I also enjoy writing programs + creating websites in my free time. I play golf and ski. One of my main intrests whe...

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!