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

  • #1
Darkmisc
213
28
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.
 

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

1. 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.

2. 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.

3. 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.

4. 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.

5. 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

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