Solved

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

Posted on 2009-06-28
7
761 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
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 85

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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