# How to replace a string/character in topic title?

**URL:** https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922
**Category:** Support
**Created:** [October 27, 2017, 10:44am UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922 "2017-10-27T10:44:12Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 10:44am UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/1 "2017-10-27T10:44:12Z")

</div>

I want to replace all the `-` in the topic **title** with a single space.

this command will do it for all the posts, how can I change it so that only a **topic-title** get affected?

` rake posts:remap["-"," "]`

---

<div class="post-metadata">

### Author: ![MakaryGo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/makarygo/32/187426_2.png) [@MakaryGo](https://meta.discourse.org/u/MakaryGo)
#### Post date: [October 27, 2017, 12:02pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/2 "2017-10-27T12:02:47Z")

</div>

As mentioned before, use [regular expressions](https://www.regular-expressions.info) 🙂

> [@Replace a string in all posts](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729/43):
>
> We’re talking about RegEx: [http://www.regular-expressions.info/](http://www.regular-expressions.info/)

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 2:04pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/3 "2017-10-27T14:04:17Z")

</div>

the regular expression for title should be something like this:  
`<title>()</title>`

how can I search in title tags in the remap command?

rake posts:remap[“-”," "]

---

<div class="post-metadata">

### Author: ![xrav3nz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/xrav3nz/32/76894_2.png) [@xrav3nz](https://meta.discourse.org/u/xrav3nz)
#### Post date: [October 27, 2017, 2:06pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/4 "2017-10-27T14:06:24Z")

</div>

`rake post:remap` does not change the title… I am not sure if any of the rake tasks does this.

You are probably better off with a one time custom Ruby script.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [October 27, 2017, 2:07pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/5 "2017-10-27T14:07:53Z")

</div>

That’s not possible using the `remap` rake task.

You’ll have to use the rails console to do that. Something like this

```ruby
rails c
Topic.exec_sql("UPDATE topics SET title = replace(title, '-', ' '), fancy_title = NULL, slug = NULL WHERE title <> replace(title, '-', ' ')")

```

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 2:22pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/6 "2017-10-27T14:22:25Z")

</div>

thanks, but this command didn’t replaced the “-” in titles with space.

any other suggestion would be appreciated.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [October 27, 2017, 2:27pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/7 "2017-10-27T14:27:19Z")

</div>

I’ve edited my query, can you try again?

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 2:32pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/8 "2017-10-27T14:32:27Z")

</div>

thanks, I tried it. the output is:

`#<PG::Result:0x00007feb4faf0298 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=6518>`

but the titles haven’t changed.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [October 27, 2017, 2:42pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/9 "2017-10-27T14:42:11Z")

</div>

Forgot to clear both `fancy_title` and `slug` columns. Query updated ⬆

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 2:45pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/10 "2017-10-27T14:45:03Z")

</div>

```plaintext
#<PG::Result:0x00007fc60eab0ca8 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>

```

no change has been made to titles 🤨

**update** : slug is set to encoded in our case, should I change it in the command? @zogstrip

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 27, 2017, 4:29pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/11 "2017-10-27T16:29:59Z")

</div>

Here’s my try. It’s not tested, so please backup first!

I don’t know how this interacts with “encoded”. Perhaps that means that dashes are encoded as something other than a `-`. If that’s the case, you’ll need to modify things accordingly.

```ruby
Topic.where("title LIKE '%-%'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

```

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 5:25pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/12 "2017-10-27T17:25:01Z")

</div>

thanks, it gave back `nil` ,

just a double check to be sure I’m doing it right; I enter the app, then run the command in the rails console.

```plaintext
sudo ./launcher enter app
rails c

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 27, 2017, 5:40pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/13 "2017-10-27T17:40:35Z")

</div>

If you got `nil`, then it didn’t find anything to replace. You need to see just what a `-` is encoded as and do that replacement.

You might do a

```
Topic.where("title LIKE '%A PHRASE FROM A TOPIC YOU KNOW NEEDS FIXING%')

```

and you can have a look at what’s in the title field. If there are lots of them, you type `q` to get back to the prompt.

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 5:49pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/14 "2017-10-27T17:49:27Z")

</div>

it seems that `-` is encoded as space in the title!

when I search for topics with space, it found me titles with space replaced with `-`.

this way is there any chance to update titles using a query?

p.s.: I hope I could explain it clearly.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 27, 2017, 5:51pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/15 "2017-10-27T17:51:33Z")

</div>

> [@Pad\_Pors](#):
>
> it seems that - is encoded as space in the title!

Then perhaps they’re fixed already or you need to reload?

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [October 27, 2017, 5:53pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/16 "2017-10-27T17:53:49Z")

</div>

thanks @pfaffman , @zogstrip

it has been solved as suggested, and I only needed to put space in the find code:

```plaintext
Topic.where("title LIKE '% %'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

```

---

<div class="post-metadata">

### Author: ![thaidb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thaidb/32/68488_2.png) [@thaidb](https://meta.discourse.org/u/thaidb)
#### Post date: [August 3, 2018, 10:18am UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/17 "2018-08-03T10:18:19Z")

</div>

> [@Pad\_Pors](#):
>
> Topic.where(“title LIKE ‘% %’”).find\_each do |topic| topic.title.gsub!(/-/," “) topic.fancy\_title.gsub!(/-/,” “) topic.slug.gsub!(/-/,” ") puts topic.title begin topic.save! rescue puts “oops, something broke.” end end

where do we put this code?  
and how? later run in terminal 🙂  
Thank!

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [August 3, 2018, 12:14pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/18 "2018-08-03T12:14:25Z")

</div>

in the rails console:

> [@Pad\_Pors](#):
>
> I enter the app, then run the command in the rails console.
> 
> ```plaintext
> sudo ./launcher enter app
> rails c
> 
> ```

---

<div class="post-metadata">

### Author: ![thaidb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thaidb/32/68488_2.png) [@thaidb](https://meta.discourse.org/u/thaidb)
#### Post date: [August 4, 2018, 4:29pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/19 "2018-08-04T16:29:35Z")

</div>

Yes in console when we run funtion, but where i insert or create new with content:

```plaintext
Topic.where("title LIKE '% %'").find_each do |topic|
  topic.title.gsub!(/-/," ")
  topic.fancy_title.gsub!(/-/," ")
  topic.slug.gsub!(/-/," ")
  puts topic.title
  begin
    topic.save!
  rescue
    puts "oops, something broke."
  end
end

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [August 5, 2018, 10:53pm UTC](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922/20 "2018-08-05T22:53:25Z")

</div>

None of this makes sense on many level.

You are searching for all titles with a SPACE and then doing a bunch of substitution on `-`

@zogstrip suggestion was the correct efficient way of doing this.

Anyway I feel this has run it’s course and any more discussion here is just adding confusion to this topic.

@thaidb you need to run `rails c` as outlined a few topics up.

[Next page](https://meta.discourse.org/t/how-to-replace-a-string-character-in-topic-title/72922.md?page=2)
