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

In summary, there are various methods to add quotation marks around strings in an array, such as using map functions in programming languages like JavaScript or Python. These methods typically involve iterating over the array and concatenating the quotation marks to each string. For example, in JavaScript, one can use `array.map(str => `"${str}"`)` to achieve this efficiently.
  • #1
Darkmisc
220
31
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.
 

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

Can I use a loop to quickly put quotation marks around strings in an array?

Yes, you can use a simple loop to iterate through each element in the array and add the quotation marks around the string.

Is there a built-in method in programming languages to add quotation marks around strings in an array?

Some programming languages like Python have built-in methods such as list comprehension or map functions that can be used to quickly add quotation marks around strings in an array.

Are there any libraries or packages that can help with putting quotation marks around strings in an array?

Yes, there are libraries or packages available in certain programming languages that provide functions specifically designed to add quotation marks around strings in an array efficiently.

How can I ensure that only strings in the array are enclosed in quotation marks?

You can check the data type of each element in the array before adding quotation marks to ensure that only strings are enclosed in quotation marks.

Is there a way to put quotation marks around strings in an array without modifying the original array?

Yes, you can create a new array or list to store the modified strings with quotation marks without altering the original array.

Similar threads

Back
Top