Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a filename, i.e., "B1_1-A.dwg".
I want this to appear as "B1" in a report that I am creating.
I an the report editor there is an option to write a code in c#.
Bt condition is that the code should be written in a single line.
Please provide some solution.

What I have tried:

I tried substring and split but I am not very good at c# so I am not sure whether I have written a correct code or not.
Posted
Updated 22-Feb-18 18:32pm
Comments
PIEBALDconsult 20-Feb-18 23:56pm    
Well, show us the code. Does it work?
phil.o 21-Feb-18 8:41am    
Please see my updated answer which now matches the substring properly.
BillWoodruff 21-Feb-18 10:38am    
"I am not very good at c#" So, start studying and experimenting instead of acting helpless.

You can use a regular expression for that.
Something like
C#
using System.Text.RegularExpressions;
// ...
private static readonly Regex r = new Regex(@"_.*", RegexOptions.Compiled | RegexOptions.CultureInvariant);
// _.? means "underscore character, followed by zero or more character"
// ...
string filename = "B1_1-A.dwg";
filename = r.Replace(filename, "");
// Here, filename is "B1"

Note: if you are using the regular expression repeatedly, it is probably best to make it a static readonly field in the class you are using it, because creating a regex repeatedly impacts the expected performance; if you make it static, the performance will be much higher.

Second version using static Replace method:
C#
using System.Text.regularExpressions;
// ...
string filename = "B1_1-A.dwg";
filename = Regex.Replace(filename, @"_.*", "");
// Here, filename is "B1"


[Edit] My bad, I misread the question. Should not answer before my second coffee, it seems. I modified the solution. [/Edit]

[Edit2] Tested it; does not match. Still searching [/Edit2]

[Edit3] Now working. Weird that it matches with * but not ?. [/Edit3]
 
Share this answer
 
v9
Comments
PIEBALDconsult 21-Feb-18 6:25am    
There's no such thing as a "line of code".
Sarita Mall 21-Feb-18 23:51pm    
I think my answer was right,which i deleted.
My Answer which i have written earlier :

filename.Split('_')[0];
phil.o 22-Feb-18 5:40am    
Yes, it was; if you would mind adding it back again it would be credited.
PIEBALDconsult 21-Feb-18 6:25am    
Or use the static method on the Regex class -- it provides performance improvements too.
phil.o 21-Feb-18 8:12am    
Are you t alking about static Replace and its overloads? I always wondered whether or not corresponding regex is constructed at each call, that's why I used to prefer defining a static variable. I must confess I have never conducted any test, though.
Thanks for the highlight.
Quote:
I have a filename, i.e., "B1_1-A.dwg".
I want this to appear as "B1"


Not sure what is the problem... Take a look at example:
C#
string filename = "B1_1-A.dwg";
int pos = filename.IndexOf("_");
string newfilename = filename.Substring(0, pos);
Console.WriteLine(newfilename);//prints: B1


Above code can be simplified to:
C#
filename.Substring(0, filename.IndexOf("_"));


In case i misunderstood your requirements, please, let me know.
 
Share this answer
 
Comments
phil.o 21-Feb-18 9:10am    
5'd.
Maciej Los 21-Feb-18 9:19am    
Thank you, Phil.
Hi ,

This is the solution which i deleted earlier.

filename.Split('_')[0];

please let me know if it not fulfill the requirement.

Regards,
Sarita
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900