Have If statement check if string contains rather than equals

agolangf · · 446 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have my current golang code contain an if statement to see if a string value equals &#34;!red&#34;, is there any way I can have the if statement check if it contains &#34;!red&#34; rather than check if it equals &#34;!red&#34; exactly?</p> <p>(an example being if Data = &#34;!red and something else&#34; it would still return true)</p> <pre><code>if (Data == &#34;!red&#34;) || (Data == &#34;!RED&#34;) { 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 &#34;f&#34; and ending in &#34;ing&#34;) then you&#39;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

446 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传