转到:从两个字符或其他字符串之间检索一个字符串 [英] Go: Retrieve a string from between two characters or other strings

查看:96
本文介绍了转到:从两个字符或其他字符串之间检索一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我有一个字符串,如下所示:

Let's say for example that I have one string, like this:

<h1>Hello World!</h1>

什么Go代码将能够从该字符串中提取Hello World!?我对Go还是比较陌生.任何帮助,我们将不胜感激!

What Go code would be able to extract Hello World! from that string? I'm still relatively new to Go. Any help is greatly appreciated!

推荐答案

有很多方法可以拆分所有编程语言中的字符串.

There are lots of ways to split strings in all programming languages.

由于我不知道您的特别要求,因此我提供了一种获取输出的示例方法 您想要从样品中得到.

Since I don't know what you are especially asking for I provide a sample way to get the output you want from your sample.

package main

import "strings"
import "fmt"

func main() {
    initial := "<h1>Hello World!</h1>"

    out := strings.TrimLeft(strings.TrimRight(initial,"</h1>"),"<h1>")
    fmt.Println(out)
}

在上面的代码中,从字符串的左侧修剪<h1>,从右侧修剪</h1>.

In the above code you trim <h1> from the left of the string and </h1> from the right.

正如我所说,有数百种拆分特定字符串的方法,但这只是使您入门的一个示例.

As I said there are hundreds of ways to split specific strings and this is only a sample to get you started.

希望有帮助,祝Golang好运:)

Hope it helps, Good luck with Golang :)

数据库

这篇关于转到:从两个字符或其他字符串之间检索一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆