Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > substring: to the end of the string

Reply
Thread Tools

substring: to the end of the string

 
 
KONTRA Gergely
Guest
Posts: n/a
 
      11-14-2003
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<(E-Mail Removed)> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-14-2003


"KONTRA Gergely" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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?


Not as far as I know. You can do str[3,str.length-3] but this isn't
really an improvement unless you value the absence of negative integers in
this expression.

robert

 
Reply With Quote
 
 
 
 
Bernard Delmée
Guest
Posts: n/a
 
      11-15-2003
> my solution:
> str[3..-1] which seems quite odd :-/


The idiom in python is str[3:]
Conversely, str[:3] gives you the 3 1st chars.

Read the explanation of slicing operations here:
http://www.python.org/doc/current/lib/typesseq.html
(read note 4 & 5)

And beware, 'str' is a builtin, hence a poor choice as a variable name.

Have fun,

Bernard.

 
Reply With Quote
 
Bernard Delmée
Guest
Posts: n/a
 
      11-15-2003
Oops - wrong NG, my bad

 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-15-2003
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?


> my solution:
> str[3..-1] which seems quite odd :-/


Seems quite even to me

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.

> What if, intuitive, we can write
> str[3..]?


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?

> Any smarter solution?


Write a String#substring method if you like.

Cheers,
Gavin


 
Reply With Quote
 
Bermejo, Rodrigo
Guest
Posts: n/a
 
      11-15-2003
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




 
Reply With Quote
 
Dan Doel
Guest
Posts: n/a
 
      11-15-2003
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


 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      11-15-2003
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"


> "123456".substring(4)

= >> "56"

> ideas ?


Regexen are overkill here.

class String
def substring(m, n=-1)
self[m..n]
end
end


Gavin


 
Reply With Quote
 
Linus Sellberg
Guest
Posts: n/a
 
      11-15-2003
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,
and making the first positive index a 1 is just as bad, for several reasons.
 
Reply With Quote
 
Jon A. Lambert
Guest
Posts: n/a
 
      11-15-2003

"KONTRA Gergely" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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?
>


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

--
J. Lambert



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to match string end for a multiline string? Peng Yu Perl Misc 2 06-24-2008 02:32 AM
Help with switch configuration, ( 3 3550's, 5 2950's end to end ) ec Cisco 3 07-25-2006 10:30 AM
String Gets Truncated: VBScript End of String Character? Mike ASP General 1 08-25-2005 02:50 PM
Measure delay end-to-end Dave Cisco 1 07-20-2004 12:51 PM
is there a difference between CIR and CIR+end to end clear channel connection? ike lozada Cisco 0 05-27-2004 02:34 AM



Advertisments