[Jprogramming] Replacing part of a string

Don Guinn donguinn at gmail.com
Tue Jul 13 21:35:31 HKT 2010


charsub only replaces characters. Use
 ('ab';'cd') stringreplace 'abhello'
cdhello
Or a shorter form, use rplc, but the arguments are reversed.
You might want to look at E. in the dictionary for finding strings if you
want to roll your own.
On Tue, Jul 13, 2010 at 7:15 AM, Mark Needham <m.h.needham at gmail.com> wrote:
> Cool, thanks!
>> So what about if you wanted to replace more than one character?
>> e.g.
>> Replace every occurrence of 'ab' with 'cd' in a string?
>> ('ab';'cd') charsub 'abhello'
>> gives a length error. I tried with 'rplc' but I haven't quite figured out
> the right way to pass arguments to that function yet
>> On 13 July 2010 14:02, Alex Rufon <alex_rufon at ist-systems.com> wrote:
>> > Hi Mark,
> >
> > As Don suggested, for this one, the standard library is your friend. :)
> >
> > load 'strings'
> > ('a';'x') charsub 'abcdefgabcdefg'
> > xbcdefgxbcdefg
> > (LF;',') charsub 'abcd',LF,'asdfasd',LF,'asdfasdf'
> > abcd,asdfasd,asdfasdf
> >
> > r/Alex
> >
> > On Jul 13, 2010, at 8:46 PM, Don Guinn wrote:
> >
> > > This is one way.
> > >
> > > y=.'abc',LF,'de',LF,'f'
> > > y=LF
> > > 0 0 0 1 0 0 1 0
> > > I.y=LF
> > > 3 6
> > > ','(I.y=LF)}y
> > > abc,de,f
> > >
> > > Look at 'system\main\strings.ijs' for several tools for manipulating
> > > strings.
> > >
> > > On Tue, Jul 13, 2010 at 6:31 AM, Mark Needham <m.h.needham at gmail.com>
> > wrote:
> > >
> > >> Hey,
> > >>
> > >> I can't work out where in the primer I need to look to work out how to
> > do
> > >> this so I figured I can post it here and someone can point me to the
> > right
> > >> place.
> > >>
> > >> I want to replace any new line '\n' characters in a string with a
> comma
> > >> ','.
> > >>
> > >> My truly horrific imperative solution looks like this:
> > >>
> > >> replaceNewLines =: 3 : 0
> > >> i =. 0
> > >> newY =. ''
> > >> while. i < # y do.
> > >> if. i { y = '\' do.
> > >> if. (i+1) { y = 'n'
> > >> do.
> > >> newY =. newY, ','
> > >> i =. i + 2
> > >> end.
> > >> else.
> > >> newY =. newY, i { y
> > >> i =. i + 1
> > >> end.
> > >> end.
> > >> newY
> > >> )
> > >>
> > >> There must be a better way to do this but I can't quite figure it out.
> > >>
> > >> Thanks in advance,
> > >> Mark
> > >> ----------------------------------------------------------------------
> > >> For information about J forums see
> http://www.jsoftware.com/forums.htm
> > >>
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>


More information about the Programming mailing list

AltStyle によって変換されたページ (->オリジナル) /