I'm new to Scala and unsure of how to achieve the following
I have the String
val output = "6055039\n3000457596\n3000456748\n180013\n"
I want to extract the numbers separated by \n and store them in an Array
output.split("\n").map(_.toInt)
Or only
output.split("\n")
if you want to keep the numbers in String format. Note that .toInt throws. You might want to wrap it accordingly.
Related
I ran a survey on Mechanical Turk, and the results were returned to me in a string formatted like this:
[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]
I'm not the sharpest programmer out there, and I'm struggling with how to extract the boolean values from the string. I only need the "true" and "false" values in the order they appear.
I would really appreciate any help!
You can use the regular expression module re to extract the values
import re
string = '[{"Q1_option1":{"Option 1":true},"Q1_option2":{"Option 2":false},"Q2_option1":{"Option 1":true},"Q2_option2":{"Option 2":false}}]'
bool_vals = re.findall("true|false", string)
print(bool_vals)
bool_vals is a list that contains the values in the order they appeared in your input string.
I have this arrayList that receives data dynamically from a database
val deviceNameList = arrayListOf<String>()
Getting the index 0 of the arraylist ie deviceNameList[0] prints a string of such a format:
[Peter, James]
How can i list all names in deviceNameList[0] individually.
Assuming your input string is [Peter, James], you could try removing the square brackets at both ends, then regex splitting on comma followed by optional whitespace.
String input = "[Peter, James]";
String[] names = input.substring(1, input.length()-1).split(",\\s*");
System.out.println(Arrays.toString(names));
This prints:
[Peter, James]
Note that Java itself places square brackets around the array contents in Arrays.toString. They are not part of the actual data.
I'm trying to replace a specific values in a long string. Is it possible to do this with replace function in python?
a snipit of the string is:
'rh':0, 'rp':0, 't':'b.nan','rh':1, 'rp':1, 't':'b.nan'
my snipit string should look like
'rh':0, 'rp':0, 't':b.nan,'rh':1, 'rp':1, 't':b.nan
i'm trying to replace the 'b.nan' to b.nan but it doesn't work.
The code i'm using:
a.replace("'b.nan'", "b.nan")
You can index strings like arrays:
string = "hello"
print(string[1])
this prints 'e'
You could try finding the index and then replacing it as such
Sorry if question is vague.
I have a string that I want to format in a certain way
Currently it gets outputted like this
Could I output this like this?
With a new line for after each deliminator?
The common deliminator is the pipe (|) for these.
You can do this with a combination of the split() function to turn the strings into arrays of elements, and UNNEST, to convert each element in the array into a separate row:
WITH t(column, text) AS (
VALUES
('column1', 'text1|text2|text3'),
('column2', 'text3|text4|text4')
)
SELECT t.column, u.item
FROM t, UNNEST(split(t.text, '|')) u(item)
I have a strings:
str = "this is a great place...."
I want to print only 30 words from this string. How to do that?
Use split and take methods:
val str = "this is a great place...."
str.split("\\W").take(30).mkString(" ")
// res0: String = this is a great place
You could just do something like:
"""(\b\w+\b\W*){0,30}""".r findPrefixOf "this is a great place...."
Or using a different notation:
"""(\b\w+\b\W*){0,30}""".r.findPrefixOf("this is a great place....")
Here is some pseudo code you can work with
Split string using the split method into an Array[String] of the words.
Iterate across the array and concatenate the words together that you want to include
Print out the string
I can't think of any external libraries or built-in functions that will do that for you. You will need to write your own code to do this.