Solved

C# Remove a certain number of characters from the end of a string.

Posted on 2009-06-28
7
771 Views
Last Modified: 2012-05-07
I have been wondering if there was an easy way to just delete the last certain number of characters from a string.

Like, I want to just remove the last 4 or 5 characters from a string.

I have found some round a bout ways to do it, but didn't know if there was a simple way to do it I am just neglecting to find?
0
Comment
Question by:dcolanduno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 50 total points
ID: 24732924
Try this for arbitrary chars. This takes 1 char off of the string.

String s = "12345";
s = s.Substring(0, s.Length - 1);
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24732936
Also, a shorthand for Substring that can also come in handy is:

s = s.Remove(3);

Removes everything from the startIndex and on, so the above would leave you with only the first 3 characters
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24733116
You could write an Extension method:
http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic3
http://msdn.microsoft.com/en-us/library/bb383977.aspx

Usage:

            string str = "abcde";
            str = str.TrimRight(2);
            MessageBox.Show(str);

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WindowsFormsApplication1
{
    public static class MyExtensions
    {
        public static String TrimRight(this String str, int NumCharsToTrim)
        {
            return NumCharsToTrim <= str.Length ? str.Substring(0, str.Length - NumCharsToTrim) : "";
        }
    }
}

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 22

Expert Comment

by:cookre
ID: 24733119
I'm a paranoid type - I'd extend mrjoltcola's way thusly:

if (s.Lenth >= NumCharsToChop)  
      s=s.Substring(0,s.Length-NumCharsToChop);
else oops...
0
 
LVL 8

Expert Comment

by:InternalStatic
ID: 24733292
Unfortunately, there is no good way to truly remove the last few characters from a String; they are "immutable", meaning that, once created, they may not be changed. In order to "Edit" a string, you always have to have a round-about way of doing so; you have to create a new String with the desired information. Substring does just that, so take your string (let's assume the String you want to "remove" characters from is declared as "String s;"), and assign it to itself using Substring:
s = s.Substring(0, s.Length - numCharsToRemove);
The first argument (0) just means that the Substring should start including characters at position 0. The next argument is how many charactesr to include, so by taking the String's length minus however many characters you want to "remove", we effectively cut off the last few characters of the String.
I realize the question was basically already answered (though not officially), but I figured you may benefit from a more detailed explanation.
Hope it helps,
Nate
0
 

Author Closing Comment

by:dcolanduno
ID: 31597750
Thank you, worked perfectly for what I was attempting to do. Wish there was a way to correct my point slip up! :|
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question