Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6334 > unrolled thread

Converting a list of strings to a single space-delimited string

Started byAndrew DeYoung <adeyoung@andrew.cmu.edu>
First post2011-11-10 12:02 +0000
Last post2011-11-11 12:34 +0000
Articles 7 — 6 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Converting a list of strings to a single space-delimited string Andrew DeYoung <adeyoung@andrew.cmu.edu> - 2011-11-10 12:02 +0000
    Re: Converting a list of strings to a single space-delimited string Ray Koopman <koopman@sfu.ca> - 2011-11-11 09:54 +0000
    Re: Converting a list of strings to a single space-delimited string David Bailey <dave@removedbailey.co.uk> - 2011-11-11 10:07 +0000
    Re: Converting a list of strings to a single space-delimited string Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-11-11 10:08 +0000
    Re: Converting a list of strings to a single space-delimited string "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-11-11 10:08 +0000
      Re: Converting a list of strings to a single space-delimited string "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-11-12 12:40 +0000
    Re: Converting a list of strings to a single space-delimited string "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-11 12:34 +0000

#6334 — Converting a list of strings to a single space-delimited string

FromAndrew DeYoung <adeyoung@andrew.cmu.edu>
Date2011-11-10 12:02 +0000
SubjectConverting a list of strings to a single space-delimited string
Message-ID<j9geh9$ovb$1@smc.vnet.net>
Hi,

I know that the command StringSplit can be used to convert a single
space-delimited string to a list of strings.  For example,

B = "1 2 3";
mylist = StringSplit[B]

gives a list mylist such that mylist = {"1", "2", "3"}.

But what if I would like to go in the reveres direction -- convert
{"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
than using a For loop and concatenating the elements of the list
together?

Thanks so much.

Andrew DeYoung
Carnegie Mellon University

[toc] | [next] | [standalone]


#6358

FromRay Koopman <koopman@sfu.ca>
Date2011-11-11 09:54 +0000
Message-ID<j9irdf$89l$1@smc.vnet.net>
In reply to#6334
On Nov 10, 4:02 am, Andrew DeYoung <adeyo...@andrew.cmu.edu> wrote:
> Hi,
>
> I know that the command StringSplit can be used to convert a single
> space-delimited string to a list of strings.  For example,
>
> B = "1 2 3";
> mylist = StringSplit[B]
>
> gives a list mylist such that mylist = {"1", "2", "3"}.
>
> But what if I would like to go in the reveres direction -- convert
> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
> than using a For loop and concatenating the elements of the list
> together?
>
> Thanks so much.
>
> Andrew DeYoung
> Carnegie Mellon University

In[1]:= s = "1 2 3";
        t = StringSplit[s]
        StringJoin@Riffle[t, " "] === s

Out[2]= {"1", "2", "3"}

Out[3]= True

[toc] | [prev] | [next] | [standalone]


#6379

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-11-11 10:07 +0000
Message-ID<j9is4f$8hq$1@smc.vnet.net>
In reply to#6334
On 10/11/2011 12:02, Andrew DeYoung wrote:
> Hi,
>
> I know that the command StringSplit can be used to convert a single
> space-delimited string to a list of strings.  For example,
>
> B = "1 2 3";
> mylist = StringSplit[B]
>
> gives a list mylist such that mylist = {"1", "2", "3"}.
>
> But what if I would like to go in the reveres direction -- convert
> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
> than using a For loop and concatenating the elements of the list
> together?
>
> Thanks so much.
>
> Andrew DeYoung
> Carnegie Mellon University
>
StringJoin @@ {"1", "2", "3"}

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#6383

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-11-11 10:08 +0000
Message-ID<j9is7b$8ib$1@smc.vnet.net>
In reply to#6334
On Nov 10, 11:02 pm, Andrew DeYoung <adeyo...@andrew.cmu.edu> wrote:
> Hi,
>
> I know that the command StringSplit can be used to convert a single
> space-delimited string to a list of strings.  For example,
>
> B = "1 2 3";
> mylist = StringSplit[B]
>
> gives a list mylist such that mylist = {"1", "2", "3"}.
>
> But what if I would like to go in the reveres direction -- convert
> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
> than using a For loop and concatenating the elements of the list
> together?
>
> Thanks so much.
>
> Andrew DeYoung
> Carnegie Mellon University

there are a few ways to do it but this is probably easiest:

list = {"1", "2", "3"};
spacer= " ";

StringJoin@Riffle[list, spacer]

Mike

[toc] | [prev] | [next] | [standalone]


#6386

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-11-11 10:08 +0000
Message-ID<j9is6c$8i3$1@smc.vnet.net>
In reply to#6334
"Andrew DeYoung" <adeyoung@andrew.cmu.edu> schrieb im Newsbeitrag 
news:j9geh9$ovb$1@smc.vnet.net...
> Hi,
>
> I know that the command StringSplit can be used to convert a single
> space-delimited string to a list of strings.  For example,
>
> B = "1 2 3";
> mylist = StringSplit[B]
>
> gives a list mylist such that mylist = {"1", "2", "3"}.
>
> But what if I would like to go in the reveres direction -- convert
> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
> than using a For loop and concatenating the elements of the list
> together?
>
> Thanks so much.
>
> Andrew DeYoung
> Carnegie Mellon University
>

It can be easily done:
(* define data *)
lst = {"1", "2", "3"}
    {"1", "2", "3"}

(* perform operation *)
StringJoin @@ lst
    "123"

(* check type of result *)
Head[%]
    String

Regards,
Wolfgang

[toc] | [prev] | [next] | [standalone]


#6407

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-11-12 12:40 +0000
Message-ID<j9lpgk$lhg$1@smc.vnet.net>
In reply to#6386
"Dr. Wolfgang Hintze" <weh@snafu.de> schrieb im Newsbeitrag 
news:j9is6c$8i3$1@smc.vnet.net...
>
> "Andrew DeYoung" <adeyoung@andrew.cmu.edu> schrieb im Newsbeitrag
> news:j9geh9$ovb$1@smc.vnet.net...
>> Hi,
>>
>> I know that the command StringSplit can be used to convert a single
>> space-delimited string to a list of strings.  For example,
>>
>> B = "1 2 3";
>> mylist = StringSplit[B]
>>
>> gives a list mylist such that mylist = {"1", "2", "3"}.
>>
>> But what if I would like to go in the reveres direction -- convert
>> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
>> than using a For loop and concatenating the elements of the list
>> together?
>>
>> Thanks so much.
>>
>> Andrew DeYoung
>> Carnegie Mellon University
>>
>
> It can be easily done:
> (* define data *)
> lst = {"1", "2", "3"}
>    {"1", "2", "3"}
>
> (* perform operation *)
> StringJoin @@ lst
>    "123"
>
> (* check type of result *)
> Head[%]
>    String
>
> Regards,
> Wolfgang
>
>
Sorry, but my solution is incorrect, because I missed the required " " 
(space) between the numbers.
Others have already given a correct solution here.

Regards,
Wolfgang

[toc] | [prev] | [next] | [standalone]


#6388

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-11-11 12:34 +0000
Message-ID<j9j4oa$a7g$1@smc.vnet.net>
In reply to#6334
On Thu, 10 Nov 2011 12:02:49 -0000, Andrew DeYoung  
<adeyoung@andrew.cmu.edu> wrote:

> Hi,
>
> I know that the command StringSplit can be used to convert a single
> space-delimited string to a list of strings.  For example,
>
> B = "1 2 3";
> mylist = StringSplit[B]
>
> gives a list mylist such that mylist = {"1", "2", "3"}.
>
> But what if I would like to go in the reveres direction -- convert
> {"1", "2", "3"} to "1 2 3"?  Is there a good way to do this, other
> than using a For loop and concatenating the elements of the list
> together?
>
> Thanks so much.
>
> Andrew DeYoung
> Carnegie Mellon University
>

In addition to the suggestions already given, one may also use:

In[1]:= myList = {"1", "2", "3"};

In[2]:= ExportString[{"1", "2", "3"}, "Words"]

Out[2]= "1 2 3"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web