Regarding strings replace functions issues

137 views
Skip to first unread message

Durga Someswararao G

unread,
Aug 27, 2019, 2:06:42 PM8/27/19
to golang-nuts
Hi,

Can anyone help me in this case.

I was reading byte data(array of bytes) from another process and converted that data to string. Now I am trying to replace escape sequences \n with strings.Replace function but golang not detecting it.

Even I tried with bytes.Replace function but no use.

Also observed even strings.Contains function also not detecting \n.

Eg:

strings.Replace("String1\nString2", "\n", "golang",-1 )

Please help to solve this Issue. Can anyone share any issues with strings package while after converting bytes to string.


burak serdar

unread,
Aug 27, 2019, 2:14:15 PM8/27/19
to Durga Someswararao G, golang-nuts
On Tue, Aug 27, 2019 at 12:06 PM Durga Someswararao G
<durgasomes...@gmail.com> wrote:
>
> Hi,
>
> Can anyone help me in this case.
>
> I was reading byte data(array of bytes) from another process and converted that data to string. Now I am trying to replace escape sequences \n with strings.Replace function but golang not detecting it.
>
> Even I tried with bytes.Replace function but no use.
>
> Also observed even strings.Contains function also not detecting \n.


Are you certain the string you are working with has those sequences?
From what you said above it sounds like it doesn't.

Maybe you can include some code and data so we can see?


>
> Eg:
>
> strings.Replace("String1\nString2", "\n", "golang",-1 )
>
> Please help to solve this Issue. Can anyone share any issues with strings package while after converting bytes to string.
>
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c3a9e503-e93d-409a-9e6f-a71af29bc8aa%40googlegroups.com.

Marvin Renich

unread,
Aug 27, 2019, 2:26:15 PM8/27/19
to golang-nuts
* Durga Someswararao G <durgasomes...@gmail.com> [190827 14:07]:
> Hi,
>
> Can anyone help me in this case.
>
> I was reading byte data(array of bytes) from another process and converted
> that data to string. Now I am trying to replace escape sequences \n with
> strings.Replace function but golang not detecting it.
>
> Even I tried with bytes.Replace function but no use.
>
> Also observed even strings.Contains function also not detecting \n.
>
> Eg:
>
> strings.Replace("String1\nString2", "\n", "golang",-1 )

This playground link does what I think you are trying to do, based on
your example: https://play.golang.org/p/VqNgO-P10oi

package main

import (
"fmt"
"strings"
)

func main() {
var s = "Hello,\nplayground"
fmt.Println(s)
s = strings.Replace(s, "\n", " *** ", -1)
fmt.Println(s)
}

If you use the playground to share a simple example of how you are
currently using this and what you want the result to be, we will be able
to help you better.

...Marvin

Martin Schnabel

unread,
Aug 27, 2019, 2:34:39 PM8/27/19
to golan...@googlegroups.com
You can use the go playground to share an example. Like this:

https://play.golang.org/p/8RJXT-e91T5

As you can see in that example both functions work as expected.
We can only guess what the problem might be without a concrete
example.

One guess would be that your input has not the newline byte, but
instead the escaped sequence '\' and 'n'? Then you would need
to use the escape sequence for the slash in the search string:

https://play.golang.org/p/O-c76dM_E5B

Durga Someswararao G

unread,
Aug 28, 2019, 3:51:52 AM8/28/19
to Martin Schnabel, golang-nuts
Hi,
Here I have added the link for my exact concern. Please consider from byte conversion to string only. Here getting as expected but while reading from another process not working with strings package.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Tamás Gulácsi

unread,
Aug 28, 2019, 5:56:13 AM8/28/19
to golang-nuts
That means that other process speaks some other encoding, not utf-8, which the strings package waits.
Use golang.org/x/text/encoding to convert to utf-8.

Durga Someswararao G

unread,
Aug 28, 2019, 6:17:47 AM8/28/19
to Tamás Gulácsi, golang-nuts
Hi Tamás,

But even in other side also I am using golang to convert string into bytes. Using tcp connection with net package I am getting data from other process.

FYI
When I try to replace string with \\n it was working fine.

Like below:
processoutput = strings.Replace("<CustomTag>String 1\n String 2</CustomTag>","\\n","'@_@_@'",-1)

On Wed, Aug 28, 2019 at 3:26 PM Tamás Gulácsi <tgula...@gmail.com> wrote:
That means that other process speaks some other encoding, not utf-8, which the strings package waits.
Use golang.org/x/text/encoding to convert to utf-8.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Michel Levieux

unread,
Aug 28, 2019, 6:23:01 AM8/28/19
to Durga Someswararao G, Tamás Gulácsi, golang-nuts
Hi,

Isn't it that from the other side, you escape the string, or call a method that does it before sending it via tcp? That would explain that all the "\n" become "\\n".

Durga Someswararao G

unread,
Aug 28, 2019, 6:34:52 AM8/28/19
to Michel Levieux, Tamás Gulácsi, golang-nuts
Hi,

Manually I tried but here my concern is shall I get the reason, Why that was happening like that?
I am trying to debug strings.Replace is not working.

Thanks.

Martin Schnabel

unread,
Aug 28, 2019, 7:15:27 AM8/28/19
to golan...@googlegroups.com
Somewhere in your code producing the text you seem to escape the
the escape sequence for the newline character.

"\n" is a string with the escape sequence for the newline character '\n'
"\\n" is a string with escaped slash and a lowercase n '\\' 'n'

Again, without seeing the code we can only guess. Here is another one:
Go has raw string literals using back-ticks `like this` those literals
do not use escape sequences (`\n` == "\\n" && `\\` == "\\\\"). Could it
be, that you accidentally used those raw string literals in the producer
code with the newline escape sequence that is not supported?



On 28.08.19 12:32, Durga Someswararao G wrote:
> Hi,
>
> Manually I tried but here my concern is shall I get the reason, Why that
> was happening like that?
> I am trying to debug strings.Replace is not working.
>
> Thanks.
>
> On Wed, Aug 28, 2019 at 3:52 PM Michel Levieux <m.le...@capitaldata.fr
> <mailto:m.le...@capitaldata.fr>> wrote:
>
> Hi,
>
> Isn't it that from the other side, you escape the string, or call a
> method that does it before sending it via tcp? That would explain
> that all the "\n" become "\\n".
>
> Le mer. 28 août 2019 à 12:17, Durga Someswararao G
> <durgasomes...@gmail.com
> <mailto:durgasomes...@gmail.com>> a écrit :
>
> Hi Tamás,
>
> But even in other side also I am using golang to convert string
> into bytes. Using tcp connection with net package I am getting
> data from other process.
>
> FYI
> When I try to replace string with \\n it was working fine.
>
> Like below:
> processoutput = strings.Replace("<CustomTag>String 1\n String
> 2</CustomTag>","\\n","'@_@_@'",-1)
>
> On Wed, Aug 28, 2019 at 3:26 PM Tamás Gulácsi
> <tgula...@gmail.com <mailto:tgula...@gmail.com>> wrote:
>
> That means that other process speaks some other encoding,
> not utf-8, which the strings package waits.
> Use golang.org/x/text/encoding
> <http://golang.org/x/text/encoding> to convert to utf-8.
>
> --
> You received this message because you are subscribed to the
> Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails
> from it, send an email to
> golang-nuts...@googlegroups.com
> <mailto:golang-nuts%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/865188f0-405e-45ee-82b9-7f6d24f1d49f%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the
> Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CA%2Ba9DGr83pA%2Bvrkg-bYkOs-qjsqxkOgEjoGTmigtJsr0hO287w%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CA%2Ba9DGr83pA%2Bvrkg-bYkOs-qjsqxkOgEjoGTmigtJsr0hO287w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CA%2Ba9DGrM47a%3Dj6Zbk1caY46FYUG7fXC2J0z0QvoJtSp69b6NCQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CA%2Ba9DGrM47a%3Dj6Zbk1caY46FYUG7fXC2J0z0QvoJtSp69b6NCQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Durga Someswararao G

unread,
Aug 28, 2019, 8:05:36 AM8/28/19
to Martin Schnabel, golang-nuts
Hi Martin,
Thanks for your help.

From first point as you said is right in my case \\n is as it is considering \n as string thanks for this.
 Second point you mentioned about back-ticks where can I get this information? Can you please share any reference links to get more knowledge about this.

Thanks.


To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2c202a4b-1db1-2247-d374-814f66c81b0c%40mb0.org.

Martin Schnabel

unread,
Aug 28, 2019, 8:23:25 AM8/28/19
to golan...@googlegroups.com
Go has a fantastic and very readable language specification with all
the details: https://golang.org/ref/spec#String_literals

(To my surprise the effective go document does not explain raw string
literals, but uses them in the web server example in the template
string)

Another guess by Michel is that you call a string quote function
like strconv.Quote either directly or indirectly on the producer side.

The following playground link demonstrates some ways you could arrive
at a quoted string. https://play.golang.org/p/-QRz7x00Shn

However, then you would also see double quotes enclosing the quoted text
in your output.

To summarize: we found out that your problem has nothing to do with
strings.Replace, but instead with the way your producer writes its
output. We have two general ideas how this could be the case but
can only help you find the issue if you share reproducible examples.

Michel Levieux

unread,
Aug 28, 2019, 8:24:16 AM8/28/19
to Durga Someswararao G, Martin Schnabel, golang-nuts

Durga Someswararao G

unread,
Aug 28, 2019, 9:15:38 AM8/28/19
to Michel Levieux, Martin Schnabel, golang-nuts
Thanks Michel.
Reply all
Reply to author
Forward
0 new messages