strings.Contains in switch-case GoLang [closed] - string

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
Is it possible to use strings.Contains in switch case?
something like:
func function(str string){
switch str {
case "str1":
...
case strings.Contains("test"):
...
default:
...
}
}
Edit: its an example, thats not the real case I need it for. Im filtering many results and I want all results that contains X and I also have cases that I need to fully match.

You can do this:
package main
import (
"fmt"
"strings"
)
func main() {
str := "testString"
switch {
case strings.Contains(str, "test"):
fmt.Println(true)
default:
fmt.Println(false)
}
}
https://go.dev/play/p/_2fMd-3kE-r
switch without argument is like switch true.

Why do you need a switch for this boolean output?
Use the following inbuilt function.
func strings.Contains(s string, substr string) bool

Related

Converting string to float using Arduino [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am trying to convert the string in my code to float but using atof() for a sub string seems not be working. Advise on how to go about it appropriately will be appreciated.
It is for a BME280 sensor using 433MHz transmitter and receiver
void loop{
if(rfrx.recv(buff, &bufflen)){
String rxstr = String((char*)buff);
for (int i=0;i=rxstr.length();i++){
if (rxstr.substring(i,i+1) == ","){
String T = rxstr.substring(0,i);
String P = rxstr.substring(i+1);
String A = rxstr.substring(i+2);
String H = rxstr.substring(i+3);
break;
}
}
}
}
Have figured it out
The .toFloat()
Thanks

String behaves weird in rust [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
let type1 = "Hello";
let mut type2 = String::from("World");
type2.push('!'); //Push can only add one single char
type2.push_str(" from Rust"); // push_str adds a string
let len = type2.len(); //Length of type2
let capacity = type2.capacity(); //Capacity in bytes
let contains = type2.contains("world"); //If contains sub string
println!("{:?}", (type1, type2, len, capacity, contains));
for words in type2.split_whitespace(){
println!("{}",words);
}
This code is giving error but if I remove the print it works just fine
In this line:
println!("{:?}", (type1, type2, len, capacity, contains));
You need to change type2 to &type2.
What you find "weird" has nothing to do with strings but the ownership/borrowing model in Rust. If you want to program in Rust, I strongly suggest you don't skip this crucial part of the language. You can start from here. Also, pay close attention to the compiler error messages. The Rust compiler usually emits incredibly accurate and useful error messages.

Custom types and their usage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Hello I have 3 custom types and then type created with these 3 types.
type Name = String
type Age = Int
type Semester = Int
type Student = (Name, Age, Semester)
I need to create function which takes student and returns his name
i have created this but it doesn't work.
getName :: Student -> Name:
getName (name_, age_, semester_) = name_
This works fine:
getName :: Student -> Name
getName (name_, age_, semester_) = name_
There shouldn't be any : or anything else in the end of the signature.
First line contains the signature by itself. Then go the lines with the definition. The type alias is Name, not Name:, so that's what should be used.

Why does sorted "tan" != "ant"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.
package main
import (
"fmt"
"sort"
)
func main() {
for _, s := range []string{"nat", "tan", "ant"} {
b := []byte(s)
sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(s, string(b))
}
}
https://play.golang.org/p/bC9QWq7aF3G
I would expect "nat", "tan" and "ant" to all be sorted to "ant", but "tan" is sorted to "atn".
Change your sort.Slice line to:
sort.Slice(b, func(i int, j int) bool { return b[i] < b[j] })
sort.Slice needs your less function to compare values in the slice in order to sort the way you intended. Your bug is that you used s rather than b in your less function.

Non.exhaustive pattern [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So im creating a function that says true if a is equal to the fst element of any pair in a list
elemMSet :: Eq a => a -> [(a,Int)] -> Bool
elemMset a [] = False
elemMSet a ((t,q):xs)| a==t = True
| otherwise = elemMSet a xs
I dont undertstand why, it shows an error of non-exhaustive pattern when i try something that should give False like :
elemMSet 'd' [('b',2), ('a',4), ('c',1)]
Error:
Tseis.hs:(4,1)-(5,48): Non-exhaustive patterns in function elemMSet
You misspelled the function name on line 2, so elemMSet only covers the non-empty case. Change the name on line 2 to elemMSet (with a capital S) and it will work fine.

Resources