0

I'm confused as to how i can create a new string on the basis of another, replacing some values of the original string,

If i have

Array(easy_id, 1_sum(term_invested_points), 1_sum(std_invested_points), 1_sum(std_used_points), 1_sum(term_used_points), 9_sum(term_invested_points))

and want to produce

Array(easy_id, 1_sum_term_invested_points_, 1_sum_std_invested_points_, 1_sum_std_used_points_, 1_sum_term_used_points_, 9_sum_term_invested_points_)

i.e substitute brackets for underscores in my array.

I have tried

array.columns.map{ case "" => "("; case x => x }

However this just produces the original array, why doesn't it work?

1 Answer 1

4

You could do something like this:

val arr = Array(
  "easy_id",
  "1_sum(term_invested_points)",
  "1_sum(std_invested_points)",
  "1_sum(std_used_points)",
  "1_sum(term_used_points)",
  "9_sum(term_invested_points)"
)

arr.map(_.replaceAll("\\(|\\)", "_"))

Map inside the array and replace all opening or closing brackets with underscores (brackets need to be escaped with backslashes as they are Regex special characters).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.