4

I have some strings like this:

  • 'Set{[5, 6, 9]}'
  • 'Set{[8, 4, "a", "[", 1]}'
  • 'Set{[4, 8, "]", "%"]}'

I want to remove the square brackets at indices 4 and -2 from these strings so that I have:

  • 'Set{5, 6, 9}'
  • 'Set{8, 4, "a", "[", 1}'
  • 'Set{4, 8, "]", "%"}'

How can I do that?

5
  • @SergioTulentsev Sorry. I edited it. Commented May 8, 2017 at 20:34
  • 4
    Why the rush to select an answer, namely the first one offered? You don't want to see others? Commented May 8, 2017 at 20:38
  • @CarySwoveland Of course I am. I'm new here and I thought I should tick an answer right away as another way to say thanks. I'll take that in mind. Commented May 8, 2017 at 20:51
  • 1
    There's no rush to select an answer. Not only might a quick selection discourage other answers, but (imo) it's discourteous to others still working on their answers. Also, on occasion the quickly-selected answer is just wrong, but there's no time for others to point that out before a selection is made. Many here wait at least a couple of hours before awarding the greenie. Commented May 8, 2017 at 20:57
  • This smells like an XY-question. What you asked for is possible, but seems to idiosyncratic. There should be a better way to solve your real problem. Commented May 9, 2017 at 8:16

2 Answers 2

5

I think you want this:

>> string = 'Set{[8, 4, "a", 6, 1]}'
=> "Set{[8, 4, \"a\", 6, 1]}"
>> string.gsub('{[', '{').gsub(']}', '}')
=> "Set{8, 4, \"a\", 6, 1}"

If there is any danger that you might see the '{[' or ']}' pattern in the middle of the string and want to preserve it there, and if you are sure of the position relative to the start and end of the string each time, you could do this:

>> string = 'Set{[8, 4, "a", 6, 1]}'
>> chars = string.chars
>> chars.delete_at(4)
>> chars.delete_at(-2)
>> chars.join
=> "Set{8, 4, \"a\", 6, 1}"
Sign up to request clarification or add additional context in comments.

1 Comment

nicely done! You got it faster than me. Removing mine hehe
1

Since you know the positions of the characters to be removed, just remove 'em. The following method does that in four steps:

  • Convert negative indices of characters to be deleted to non-negative indices.
  • Sort the indices.
  • Use Array#reverse_each to process the indices in reverse order.
  • Remove the character at each index.

def remove_chars(str, *indices)      
  sz = str.size
  indices.map { |i| i >= 0 ? i : sz + i }.sort.reverse_each { |i| str[i] = '' }
  str
end

puts remove_chars('Set{[5, 6, 9]}', 4, -2 )
Set{5, 6, 9}

puts remove_chars('Set{[8, 4, "a", "[", 1]}', 4, -2 )
Set{8, 4, "a", "[", 1}

puts remove_chars('Set{[4, 8, "]", "%"]}', 4, -2 )
Set{4, 8, "]", "%"}

puts remove_chars('Set{[8, 4, "a", "[", 1]}', 23, 4, -2 )
Set{8, 4, "a", "[", 1

In the last example the string size is 24.

This method mutates the string on which it operates. If the string is not to be altered, execute

remove_chars(str.dup, 4, -2 )

or add a first line to the method str_cpy = str.dup and operate on str_cpy`.

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.