Is there a quick way to put quotation marks around strings in an array?

  • #1
Darkmisc
204
27
TL;DR Summary
I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?
Hi everyone

I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?

Thanks
 
Technology news on Phys.org
  • #2
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
 
  • Like
Likes FactChecker and Darkmisc
  • #3
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
 
  • Like
Likes Darkmisc
  • #4
jedishrfu said:
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
This code is basically just splitting the words. The OP also says that they want quotation marks around each word. I presume that when the OP parses that list of words, for each element in that list, they would want the character at positions 0 and -1 to be '. This can be achieved by a simple modification of your code:
Python:
>>> d = ["\'" + word + "\'" for word in s.split(" ")]
>>> d
["'aaa'", "'bbb'", "'ccc'", "'ddd'", "'eee'", "'fff'"]
>>> (d[0])[0]
"'"
>>> (d[0])[-1]
"'"
 
  • Like
Likes Darkmisc
  • #5
Vanadium 50 said:
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
Some care is needed with word processors, which sometimes replace quotes with the better looking "smart quotes". But I'd do similar with :s/,/","/ in vi. 😁
 
  • Like
Likes Darkmisc
  • #6
Ibix said:
in vi.
I was going to suggest sed. Usually sed won't solve your problems - but it will put them in perspective.
 
  • #7
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
 
  • Like
Likes Darkmisc
  • #8
jtbell said:
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
For now, it's just once. Easiest method was Vanadium50's. I'd completely forgotten that I could use something other than Godot to solve the problem.
 

1. How can I quickly add quotation marks to each string in an array using JavaScript?

You can use the map function to iterate over each element in the array and add quotation marks. For example: let arrayWithQuotes = array.map(item => \`"\${item}"\`);

2. Is there a method in Python to put quotation marks around all strings in a list?

In Python, you can use list comprehension to achieve this. For instance: array_with_quotes = ['"{}"'.format(i) for i in array] or using f-strings: array_with_quotes = [f'"{i}"' for i in array].

3. Can I automatically add quotes to strings in an array in PHP?

Yes, you can use the array_map function in PHP. Here's how you could do it: $arrayWithQuotes = array_map(function($item) { return "\"$item\""; }, $array);

4. What is the simplest way to enclose array elements with quotes in Ruby?

In Ruby, you can use the map method with a simple syntax: array_with_quotes = array.map { |item| "\"#{item}\"" }.

5. How do I add quotation marks to each element of an array in C#?

You can use LINQ in C# to add quotes to each element of an array. For example: var arrayWithQuotes = array.Select(item => $"\"{item}\"").ToArray();

Similar threads

  • Programming and Computer Science
Replies
1
Views
542
  • Programming and Computer Science
Replies
4
Views
829
  • Programming and Computer Science
Replies
1
Views
694
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
3
Views
318
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
11K
  • Programming and Computer Science
Replies
15
Views
2K
Back
Top