Ask a Question related to Ruby, Design and Development.
-
KONTRA Gergely #1
substring: to the end of the string
Hi!
Is there any easiest way to get the substring starting from the 3rd
character?
my solution:
str[3..-1] which seems quite odd :-/
What if, intuitive, we can write
str[3..]?
Any smarter solution?
Gergo
--
+-[ Kontra, Gergely<kgergely@mcl.hu> PhD student Room IB113 ]---------+
| [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok, hogy |
| Mobil
+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: [url]http://hu.php.net[/url] --+
KONTRA Gergely Guest
-
Using a substring in SQL WHERE
Hi all. Need a little help getting my WHERE right. I have a short date field in my Access database called eventdate. I need to form my WHERE... -
AREs in substring(from)
As the documentation leads me to expect, my Postgresql 7.4 installation produces: select substring('Bar, Foo' FROM '.') -> 'B' but even... -
Substring question
I am trying to select a part of a text field based on a regular expression, the data looks like this Rv0001c_f Rv0002_r Rv1003c_r Etc I... -
Testing for substring
I have a news headline grabbed from another page in the same site. The data is captured like so: $post=mysql_fetch_row($res2); $post=$post; I... -
Substring Matching
> I'm relatively new to SQL Server, and I was just wondering if anyone had a SELECT * FROM table WHERE SUBSTRING(column, 1, 4) = 'anti' SELECT... -
Robert Klemme #2
Re: substring: to the end of the string
"KONTRA Gergely" <kgergely@mlabdial.hit.bme.hu> schrieb im Newsbeitrag
news:20031114154849.GA12384@mlabdial.hit.bme.hu...Not as far as I know. You can do str[3,str.length-3] but this isn't> Hi!
>
> Is there any easiest way to get the substring starting from the 3rd
> character?
>
> my solution:
> str[3..-1] which seems quite odd :-/
>
> What if, intuitive, we can write
> str[3..]?
>
> Any smarter solution?
really an improvement unless you value the absence of negative integers in
this expression. :-)
robert
Robert Klemme Guest
-
Bernard Delmée #3
Re: substring: to the end of the string
> my solution:
The idiom in python is str[3:]> str[3..-1] which seems quite odd :-/
Conversely, str[:3] gives you the 3 1st chars.
Read the explanation of slicing operations here:
[url]http://www.python.org/doc/current/lib/typesseq.html[/url]
(read note 4 & 5)
And beware, 'str' is a builtin, hence a poor choice as a variable name.
Have fun,
Bernard.
Bernard Delmée Guest
-
Bernard Delmée #4
Re: substring: to the end of the string
Oops - wrong NG, my bad ;-)
Bernard Delmée Guest
-
Gavin Sinclair #5
Re: substring: to the end of the string
On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
> Hi!> Is there any easiest way to get the substring starting from the 3rd
> character?Seems quite even to me> my solution:
> str[3..-1] which seems quite odd :-/
Seriously, the ability to negative-index strings and arrays like that
is a breath of fresh air compared to traditional compiled languages.
I see no need to improve on it.
I rather doubt that's going to happen. When you execute str[3..7],> What if, intuitive, we can write
> str[3..]?
the 3..7 isn'y arbitrary syntax, it's a Range object. If Range
objects were allowed to be unbounded, then fine. So the question
really becomes, should Range objects allow unbounded ranges?
Write a String#substring method if you like.> Any smarter solution?
Cheers,
Gavin
Gavin Sinclair Guest
-
Bermejo, Rodrigo #6
Re: substring: to the end of the string
class String
def substring
/(^...)(.*)\z/ =~ self
return $2
end
end
"123456".substring
= >"456"
Now......
I am just wondering how can I build dinamically the Regexp
I mean ...
"123456".substring(5)
= > "6"
"123456".substring(4)
= > "56"
ideas ?
-r.
KONTRA Gergely wrote:
-->Hi!
>
>Is there any easiest way to get the substring starting from the 3rd
>character?
>
>my solution:
>str[3..-1] which seems quite odd :-/
>
>What if, intuitive, we can write
>str[3..]?
>
>Any smarter solution?
>
>Gergo
>
>
General Electric - CIAT
Advanced Engineering Center
________________________________________
Rodrigo Bermejo
Information Technologies.
Special Applications
Dial-comm : *879-0644
Phone
+52) 442-196-0644
Bermejo, Rodrigo Guest
-
Dan Doel #7
Re: substring: to the end of the string
Some ways:
class String
def substring1(n)
reg = Regexp.compile("(^" + "."*n + ")(.*)\\z")
reg.match(self)[2]
end
def substring2(s, e=-1)
self[s..e]
end
end
p "12345".substring1 1
p "12345".substring1 2
p "12345".substring2 1
p "12345".substring2 2
p "12345".substring2 1, 3
Dan Doel Guest
-
Gavin Sinclair #8
Re: substring: to the end of the string
On Saturday, November 15, 2003, 12:24:42 PM, Rodrigo wrote:
> class String
> def substring
> /(^...)(.*)\z/ =~ self
> return $2
> end
> end> "123456".substring
> = >"456"> Now......> I am just wondering how can I build dinamically the Regexp
> I mean ...> "123456".substring(5)
> = > "6"= >> "56"> "123456".substring(4)
Regexen are overkill here.> ideas ?
class String
def substring(m, n=-1)
self[m..n]
end
end
Gavin
Gavin Sinclair Guest
-
Linus Sellberg #9
Re: substring: to the end of the string
Gavin Sinclair wrote:
I do, but I have no good solution to the problem.> On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
> Seriously, the ability to negative-index strings and arrays like that
> is a breath of fresh air compared to traditional compiled languages.
> I see no need to improve on it.
The problem being that positive indexes go from 0 to n-1, while negative
go from -1 to -n, which could introduce confusion.
But as I said, I know of no good solutions, since -0 is not a good way,
and making the first positive index a 1 is just as bad, for several reasons.
Linus Sellberg Guest
-
Austin Ziegler #10
Re: substring: to the end of the string
On Sat, 15 Nov 2003 10:24:42 +0900, Bermejo, Rodrigo wrote:
As Gavin pointed out, regexen are overkill here. However, the> class String
> def substring
> /(^...)(.*)\z/ =~ self
> return $2
> end
> end
technique is worth noting:
class String
def substring(start, len = nil)
if len.nil?
/^.{#{start}}(.*)/m =~ self
else
/^.{#{start}}(.{#{len}})/m =~ self
end
$1
end
end
"123456".substring(3) # => "456"
"123456".substring(3, 2) # => "45"
-austin
--
austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.15
* 09.51.26
Austin Ziegler Guest
-
Jon A. Lambert #11
Re: substring: to the end of the string
"KONTRA Gergely" <kgergely@mlabdial.hit.bme.hu> wrote in message news:20031114154849.GA12384@mlabdial.hit.bme.hu...Actually I find this behavior annoying:> Hi!
>
> Is there any easiest way to get the substring starting from the 3rd
> character?
>
> my solution:
> str[3..-1] which seems quite odd :-/
>
> What if, intuitive, we can write
> str[3..]?
>
> Any smarter solution?
>
irb(main):015:0> "hello"[3..-1]
=> "lo"
irb(main):016:0> "hello"[4..-1]
=> "o"
irb(main):017:0> "hello"[5..-1]
=> ""
irb(main):018:0> "hello"[6..-1]
=> nil
--
J. Lambert
Jon A. Lambert Guest
-
Josef 'Jupp' SCHUGT #12
Re: substring: to the end of the string
Hi!
* KONTRA Gergely; 2003-11-14, 23:30 UTC:Besids 'get used to -1'? Well, here's some extension to 'String'. I> Is there any easiest way to get the substring starting from the 3rd
> character?
>
> my solution:
> str[3..-1] which seems quite odd :-/
>
> What if, intuitive, we can write
> str[3..]?
>
> Any smarter solution?
did try my best to follow POLS. The intention is that left, right,
mid emulate BASIC's left$, right$ and mid$ while head and tail
without any arguments have the results they should have from a
functional programming point of view.
class String
# 'number' leftmost chars
def left(number = 1)
self[0..number-1]
end
# 'number' rightmost chars
def right(number = 1)
self[-number..-1]
end
# 'number' chars starting at position 'from'
def mid(from, number=1)
self[from..from+number]
end
# chars from beginning to 'position'
def head(position = 0)
self[0..position]
end
# chars following 'position'
def tail(position = 0)
self[position+1..-1]
end
end
Josef 'Jupp' Schugt
--
.-------.
message > 100 kB? / | |
sender = spammer? / | R.I.P.|
text = spam? / ___| |___
Josef 'Jupp' SCHUGT Guest
-
Simon Strandgaard #13
Re: substring: to the end of the string
On Sun, 16 Nov 2003 06:04:51 +0900, Josef 'Jupp' SCHUGT wrote:
[snip class String extension]> Besids 'get used to -1'? Well, here's some extension to 'String'. I
> did try my best to follow POLS. The intention is that left, right,
> mid emulate BASIC's left$, right$ and mid$ while head and tail
> without any arguments have the results they should have from a
> functional programming point of view.
>
Useful.. I have added your snippet at rubygarden (StringSub):
[url]http://www.rubygarden.org/ruby?StandardClassExtensions/String[/url]
--
Simon Strandgaard
Simon Strandgaard Guest
-
Simon Strandgaard #14
Re: substring: to the end of the string
On Sun, 16 Nov 2003 06:04:51 +0900, Josef 'Jupp' SCHUGT wrote:
> class String
> def left(number = 1)
> self[0..number-1]
> end
> def right(number = 1)
> self[-number..-1]
> end
> def mid(from, number=1)
> self[from..from+number]
> end
> def head(position = 0)
> self[0..position]
> end
> def tail(position = 0)
> self[position+1..-1]
> end
> end
Maybe some destructive versions of the above methods could be useful?
input="diamonds are forever"
p input.right!(4) #-> "ever"
p input.left!(4) #-> "diam"
p input # -> "onds are for"
--
Simon Strandgaard
Simon Strandgaard Guest
-
nobu.nokada@softhome.net #15
Re: substring: to the end of the string
Hi,
At Sat, 15 Nov 2003 23:58:55 +0900,
Austin Ziegler wrote:Another syntax sugar,> As Gavin pointed out, regexen are overkill here. However, the
> technique is worth noting:
self[/\A.{#{start}}(.*)/m, 1]> class String
> def substring(start, len = nil)
> if len.nil?self[/\A.{#{start}}(.{#{len}})/m, 1]> else--> end
> $1
> end
> end
Nobu Nakada
nobu.nokada@softhome.net Guest
-
Austin Ziegler #16
Re: substring: to the end of the string
On Mon, 17 Nov 2003 11:40:13 +0900, Jon A. Lambert wrote:
This has to do with how Ruby views indices on arrays and array-like> Actually I find this behavior annoying:
> irb(main):015:0> "hello"[3..-1]
> => "lo"
> irb(main):016:0> "hello"[4..-1]
> => "o"
> irb(main):017:0> "hello"[5..-1]
> => ""
> irb(main):018:0> "hello"[6..-1]
> => nil
structures (like strings).
[h] [e] [l] [l] [o]
0 1 2 3 4 5
The indices are "before" the actual element, so (5 .. -1) represents
"end of the string to end of the string", whereas (6 .. -1)
reperesents (outside fo the string to the end of the string).
Some of this has to do with performance, I think, but it's something
that makes sense, once you're used to it. I think that it's more
sensible than the typical implementations.
-austin
--
austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.16
* 23.27.28
Austin Ziegler Guest
-
Robert Klemme #17
[OT] Re: substring: to the end of the string
"Linus Sellberg" <linse428@student.liu.se> schrieb im Newsbeitrag
news:bp53n1$4o7$1@news.island.liu.se...This reminds me of an old mathematician's joke: there was this professor> Gavin Sinclair wrote:>> > On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
> > Seriously, the ability to negative-index strings and arrays like that
> > is a breath of fresh air compared to traditional compiled languages.
> > I see no need to improve on it.
> I do, but I have no good solution to the problem.
>
> The problem being that positive indexes go from 0 to n-1, while negative
> go from -1 to -n, which could introduce confusion.
>
> But as I said, I know of no good solutions, since -0 is not a good way,
for math that found an epsilon so small, that half of it was already
negative... ;-)
reasons.> and making the first positive index a 1 is just as bad, for several
Yup
robert
Robert Klemme Guest
-
KONTRA Gergely #18
Re: substring: to the end of the string
On 1115, Gavin Sinclair wrote:
Yes, it should> I rather doubt that's going to happen. When you execute str[3..7],
> the 3..7 isn'y arbitrary syntax, it's a Range object. If Range
> objects were allowed to be unbounded, then fine. So the question
> really becomes, should Range objects allow unbounded ranges?
What about 3..INF here?
--
+-[ Kontra, Gergely<kgergely@mcl.hu> PhD student Room IB113 ]---------+
| [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok, hogy |
| Mobil
+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: [url]http://hu.php.net[/url] --+
KONTRA Gergely Guest
-
Jason DiCioccio #19
Re: substring: to the end of the string
--On Saturday, November 15, 2003 12:48 AM +0900 KONTRA Gergely
<kgergely@mlabdial.hit.bme.hu> wrote:
I typically use str[3..str.length].. It's probably slower, but it looks a> Hi!
>
> Is there any easiest way to get the substring starting from the 3rd
> character?
>
> my solution:
> str[3..-1] which seems quite odd :-/
bit cleaner in code..
Regards,
-JD-
Jason DiCioccio Guest



Reply With Quote

