6 Replies - 760 Views - Last Post: 10 December 2013 - 01:24 AM Rate Topic: -----

#1 markhaloce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Trim everything after a special character

Posted 09 December 2013 - 01:21 AM

I have a text in the textbox1. Imagine the text is "abcdefgh*ijklmnopqrstuvwxyz"

I want to remove all the characters after the character "*" in that textbox.

the output I want is "abcdefgh"

how can I do it with C# ?
Is This A Good Question/Topic? 0
  • +

Replies To: Trim everything after a special character

#2 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

Re: Trim everything after a special character

Posted 09 December 2013 - 01:26 AM

You could use the split method, use it on * char, that will split string into array with 2 elements
abcdefgh and ijklmnopqrstuvwxyz the you can simply remove element at index 1 since that is what you want.
Was This Post Helpful? 2
  • +
  • -

#3 markhaloce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: Trim everything after a special character

Posted 09 December 2013 - 01:28 AM

View PostMichael26, on 09 December 2013 - 01:26 AM, said:

You could use the split method, use it on * char, that will split string into array with 2 elements
abcdefgh and ijklmnopqrstuvwxyz the you can simply remove element at index 1 since that is what you want.


Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,700
  • Joined: 05-May 12

Re: Trim everything after a special character

Posted 09 December 2013 - 07:30 AM

Although using Split() would be a quick and easy way to get what you want, it is also wasteful in terms of resources. Remember that Split() will allocate a new array, and that array will now contain two new strings.

It would be more efficient to get the IndexOf() the asterisk, and then use Substring() to get the part of the string that you want. The result of the IndexOf() would at worse just use 4 bytes on the stack, and the Substring() will only allocate one new string.
Was This Post Helpful? 4
  • +
  • -

#5 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

Re: Trim everything after a special character

Posted 09 December 2013 - 08:52 AM

Well yes, but that new array would have only 2 elements so i don't think it's a big waste. When i'm working with something that small i don't consider it a waste at all, this isn't the '70, we don't need to preserve memory in this case.
Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: Trim everything after a special character

Posted 09 December 2013 - 05:12 PM

Programming is always a balance of logical efficiency and programmer efficiency. Specific project requirements will tip the scales either way, but when either solution is similar in lines of code and difficulty, it's typically better to choose the more efficient algorithm. You'd likely never notice the difference in a small application, but if he were doing this over millions of records, the difference would become apparent. It's not just the memory overhead, it's the set-up and tear-down overhead. Creating an array just to grab the first index, then allowing it to be GCed is extra cycles that could be skipped. Other potential issues: what if there's more than one instance of the special character? Then the arrays start getting bigger.

Realistically, either is a valid solution. Practically, we should favor efficiency when it's not a case of premature optimization. This actually piqued my curiosity to see just how much more efficient it is, so here's my test code:

Spoiler


Quote

Avg ticks for substring method: 0.72752
Avg ticks for split method: 1.57559


Of course, ticks are super short, so the fact that one about twice as fast isn't all that important, but it is telling, no?
Was This Post Helpful? 3
  • +
  • -

#7 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,506
  • Joined: 08-April 09

Re: Trim everything after a special character

Posted 10 December 2013 - 01:24 AM

Quote

but if he were doing this over millions of records, the difference would become apparent.

Quote

Other potential issues: what if there's more than one instance of the special character? Then the arrays start getting bigger.


I didn't think about that, i was working with what OP presented to me. If that was part of something bigger then i would consider those implication but they were not apparent in this case.

But thanks anyway Curtis.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1