Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 268025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:38:10+00:00 2026-05-11T23:38:10+00:00

I’d like to trim these purchase order file names (a few examples below) so

  • 0

I’d like to trim these purchase order file names (a few examples below) so that everything after the first “_” is omitted.

INCOLOR_fc06_NEW.pdf
Keep: INCOLOR (write this to db as the VendorID) Remove: _fc08_NEW.pdf

NORTHSTAR_sc09.xls
Keep: NORTHSTAR (write this to db as the VendorID) Remove: _sc09.xls

Our scenario: The managers are uploading these files to our Intranet web server, to make them available to download/view ect. I’m using Brettles NeatUpload, and for each file uploaded, am writing the files attributes into the PO table (sql 2000). The first part of the file name will be written to the DB as a VendorID.

The naming convention for these files is consistent in that the the first part of the file is always the vendor name (or Vendor ID) followed by an “_” then other unpredictable chars used to identify the type of Purchase Order then the file extention – which is consistently either .xls, .XLS, .PDF, or .pdf.

I tried TrimEnd – but the array of chars that you have to provide ends up being long and can conflict with the part of the file name I want to keep. I have a feeling I’m not using TrimEnd properly.

What is the best way to use string.TrimEnd (or any other string manipulation in C#) that will strip off all chars after the first “_” ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:38:11+00:00Added an answer on May 11, 2026 at 11:38 pm

    I’ll probably offend the anti-regex lobby, but here I go (ducking):

    string stripped = Regex.Replace(filename, @"(?<=[^_]*)_.*",String.Empty);
    

    This code will strip all extra characters after the first '_', unless there is no '_' in the string (then it will just return the original string).

    It’s one line of code. It’s slower than the more elaborate IndexOf() algorithm, but when used in a non-performance-sensitive part of the code, it’s a good solution.

    Get your flame-throwers out…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.