---
title: "TrimEnd() Vs. SubString()"
url: "https://osherove.com/blog/2004/4/29/trimend-vs-substring.md"
canonical: "https://osherove.com/blog/2004/4/29/trimend-vs-substring.html"
published: "2004-04-29T22:59:00"
updated: "2004-04-29T22:59:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "Many developers have had to write a loop that writes out a comma separated value list . There's always the trick of how you make that last comma get removed from the end of that string. Usually, at the end of the loop,…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# TrimEnd() Vs. SubString()

By Roy Osherove April 29, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published April 29, 2004; technical details may be outdated.

Many developers have had to [write a loop that writes out a comma separated value list](http://weblogs.asp.net/dburke/archive/2004/04/28/122332.aspx). There's always the trick of how you make that last comma get removed from the end of that string. Usually, at the end of the loop, many developers will do something like this to remove the last char in the string:

_out = out.Substring( 0, out.LastIndexOf( ',' )_

However, there's a **cleaner** way to accomplish this task:

_out = out.TrimEnd(',')_

_TrimeEnd_ can receive a char array so you can remove any unwanted chars from the end easily. The code is also more readable. And yes, there's a TrimStart() to match that function.

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- May 7, 2004

    ### [Back from Tech-Ed, and another Israeli .Net blogger found.](https://osherove.com/blog/2004/5/7/back-from-tech-ed-and-another-israeli-net-blogger-found.html)

- May 1, 2004

    ### [Book of the month: May](https://osherove.com/blog/2004/5/1/book-of-the-month-may.html)

- Apr 29, 2004

    ### [Why is VS.Net Add-in architecture so overly complicated?](https://osherove.com/blog/2004/4/29/why-is-vsnet-add-in-architecture-so-overly-complicated.html)

- Apr 29, 2004

    ### [How to: Creating project groups under a single solution](https://osherove.com/blog/2004/4/29/how-to-creating-project-groups-under-a-single-solution.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
