Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > string to substring??

Reply
Thread Tools

string to substring??

 
 
Pat Kiatchaipipat
Guest
Posts: n/a
 
      12-03-2007
I have one string name

message = "5x-2=18"

and I want to create substring or array by this

mesArray[0] = 5
mesArray[1] = x
mesArray[2] = -
...

this has function?
how can I make it?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ilan Berci
Guest
Posts: n/a
 
      12-03-2007
Pat Kiatchaipipat wrote:
> I have one string name
>
> message = "5x-2=18"
>
> and I want to create substring or array by this
>
> mesArray[0] = 5
> mesArray[1] = x
> mesArray[2] = -
> ...
>
> this has function?
> how can I make it?


irb(main):001:0> mesArray = "5x-2=18".split ""
=> ["5", "x", "-", "2", "=", "1", "8"]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-03-2007
2007/12/3, Pat Kiatchaipipat <(E-Mail Removed)>:
> I have one string name
>
> message = "5x-2=18"
>
> and I want to create substring or array by this
>
> mesArray[0] = 5
> mesArray[1] = x
> mesArray[2] = -
> ...
>
> this has function?
> how can I make it?


You have that already in your String:

irb(main):001:0> message = "5x-2=18"
=> "5x-2=18"
irb(main):002:0> message[0].chr
=> "5"
irb(main):003:0> message[1].chr
=> "x"
irb(main):004:0> message[2].chr
=> "-"

I am suspecting though that what you really want is a parser for
expressions. A simplistic version could look like this:

irb(main):005:0> message.scan %r{\d+|\w+|[-+=]}
=> ["5", "x", "-", "2", "=", "18"]

If you want to properly process expressions with brackets etc. you
need a more complex solution.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Pat Kiatchaipipat
Guest
Posts: n/a
 
      12-03-2007
thank you robert
--
Posted via http://www.ruby-forum.com/.

 
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
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments