I love C# but sometimes its just ridiculous:
Page 1 of 1
tainted4ever
VIP Member



Posts: 11336

PostPosted: Thu, 22nd Jul 2010 03:15    Post subject: I love C# but sometimes its just ridiculous:
Case in point:

Code:
string extractedKeyword = s.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries)[0];


Some of my code from today. Any way to make it neater/more compact? I think this is pretty ridiculous Laughing
Back to top
panzieman




Posts: 288

PostPosted: Thu, 22nd Jul 2010 04:10    Post subject:
You should try doing basic math in scheme before you complain about "ridiculous" code.

And I can't think of anything you can really do to compact that. You can't get rid of any arguments or anything. Perhaps if you're splitting strings exactly the same way a lot you could make a small method that returns the split string and then just call that method instead of writing out all that.
Back to top
tainted4ever
VIP Member



Posts: 11336

PostPosted: Thu, 22nd Jul 2010 04:15    Post subject:
panzieman wrote:
You should try doing basic math in scheme before you complain about "ridiculous" code.
I've had 2 years of Scheme . One year in High School, one year in College. I'm done with that language thank your very much!

Quote:

And I can't think of anything you can really do to compact that. You can't get rid of any arguments or anything. Perhaps if you're splitting strings exactly the same way a lot you could make a small method that returns the split string and then just call that method instead of writing out all that.
I would but its only used once.
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73238
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Thu, 22nd Jul 2010 14:17    Post subject:
There is also Regex.Split(str, ",") but I can't find anything to remove empty entries...
Back to top
garus
VIP Member



Posts: 34197

PostPosted: Thu, 22nd Jul 2010 15:32    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:27; edited 1 time in total
Back to top
tainted4ever
VIP Member



Posts: 11336

PostPosted: Thu, 22nd Jul 2010 23:21    Post subject:
I hate regex Mad
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73238
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Thu, 22nd Jul 2010 23:41    Post subject:
garus wrote:
PS. Every language I use reminds of how awsome Ruby is Sad
Same thing in Ruby:
Code:

text = "some, kind, of text, here"
puts text.split(',').delete("").first

Very Happy

You can do exactly the same in C#:
Code:
var test = "123,456,,789";
var results = test.Split(',').Where(str => str != "").First();


My IMDb Ratings | Fix NFOHump Cookies | Hide Users / Threads | Embedded Content (Videos/GIFs/Twitter/Reddit) | The Derps Collection

Death smiles at us all; all we can do is smile back.


Last edited by LeoNatan on Thu, 22nd Jul 2010 23:43; edited 1 time in total
Back to top
me7




Posts: 3942

PostPosted: Thu, 22nd Jul 2010 23:41    Post subject:
tainted4ever wrote:
I hate regex Mad

Could you point us to something that you don't hate and maybe someone can work a solution around it Razz
Back to top
garus
VIP Member



Posts: 34197

PostPosted: Thu, 22nd Jul 2010 23:44    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:27; edited 1 time in total
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73238
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Thu, 22nd Jul 2010 23:48    Post subject:
Could even improve it to:

Code:
var result = test.Split(',').Where(str => str.Trim() != "").First();


This will take care of cheeky results like "123, ,234". Razz
Back to top
tainted4ever
VIP Member



Posts: 11336

PostPosted: Fri, 23rd Jul 2010 03:01    Post subject:
me7 wrote:
tainted4ever wrote:
I hate regex Mad

Could you point us to something that you don't hate and maybe someone can work a solution around it Razz
Ice cream Very Happy
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group