<p>I have my current golang code contain an if statement to see if a string value equals "!red", is there any way I can have the if statement check if it contains "!red" rather than check if it equals "!red" exactly?</p>
<p>(an example being if Data = "!red and something else" it would still return true)</p>
<pre><code>if (Data == "!red") || (Data == "!RED") {
return true
}
</code></pre>
<hr/>**评论:**<br/><br/>jmank88: <pre><p><a href="https://golang.org/pkg/strings/#Contains" rel="nofollow">https://golang.org/pkg/strings/#Contains</a></p></pre>Fwippy: <pre><p>You may also be interested in <code>strings.ToLower</code>, so that you can search for red, RED, rED, or any other capitalization, without writing them all out yourself.</p></pre>PsyWolf: <pre><p>As other commented said, for simple cases like this, you want string.Contains and string.Lower</p>
<p>If you start to need something more complex (like any word starting with "f" and ending in "ing") then you'll want to learn about regular expressions, aka regex. Go has a package for them in the standard libs.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传