tags:

views:

553

answers:

5

How would I truncate a sentence at a certain character:

$sentence = 'Stack Overflow - Ask Questions Here';

so that only the following is echoed:

Stack Overflow

The character count varies, but the stop point is always "Space Dash Space"

A: 

This would be highly dependent upon what language you are using...

Drakonite
A: 

Although you didn't mention a language, I am going to guess Perl due to the $variable name. In Perl one of the easiest ways to do this is using a simple regular expression:

$sentence = 'Stack Overflow - Ask Questions Here';

if ($sentence =~ /^(.*?) - /) {
  print "Found match: '$1'\n";
}

This matches the first part of the string, in a non-greedy fashion, up until the first space-dash-space sequence. The parenthesis around the first part of the expression indicates that the matching part should be "captured", in Perl it will be stored in the variable $1 (other captured patterns are stored into $2, $3, etc). If a match is found, the matching part is stored into $1 and then printed.

Robert Gamble
+1  A: 

If using python a non-regexp approach would be:

>>> s = 'Stack Overflow - Ask Questions Here'  
>>> s.split(' - ')  
['Stack Overflow', 'Ask Questions Here']  
>>> # To get the substring before the match  
>>> s.split(' - ')[0]  
'Stack Overflow'

A regexp approach might be:

>>> import re
>>> re.split(' - ', s)[0]
'Stack Overflow'

Of course, you could build a regexp to match the entire string with your expected token, and group the first portion but given these two methods that is more work than is necessary.

Rob Cowie
The Split would work with a variety of languages.
Brody
A: 

Assuming Perl, try this:

$sentence1 = 'Stack Overflow - Ask Questions Here - And more here';
$sentence2 = 'Just Stack Overflow';

$sentence1 =~ /^(.*?)( - |$)/;
print $1, "\n";

$sentence1 =~ /^(?|(.*) - |(.*)$)/;
print $1, "\n";

$sentence2 =~ /^(.*?)( - |$)/;
print $1, "\n";

$sentence2 =~ /^(?|(.*) - |(.*)$)/;
print $1, "\n";

These will match up to the first or the last " - ", or the whole string if there's no " - "

Brent.Longborough
A: 

ah, it is for PHP. it seemed that strstr() would would but it only returns everything after the Space Dash Space. super thanks for all your help so far!

~ Bill