Delete from line(String) unicode symbol

For example:

let s= Sring::from(❤)
s.trim_matches!(char::is_alphabetic & char::is_numeric);//.trim_matches(❤);
//(!char::is_alphabetic & !char::is_numeric)

I want delete all such symbols or only :heart:, example above give error:
1)error: no implementation for fn(char) -> bool {char::methods::<impl char>::is_alphabetic} & fn(char) -> bool {char::methods::<impl char>::is_numeric}
2)possible error: expected bool found fn(char) -> bool

The symbol is not a valid char “as we know it”. Here is a link that describes the issue.

I also created a playground example that includes a filter to remove image/emoji-like symbols.

let my_str = "test 📖 one ☎️ two 東 three".to_string();

// allow for chars that require 1-4 8-bit code-units
let clean_str: String = my_str
    .filter(|c| c.is_alphanumeric() || c.is_whitespace())

// or, assume valid ascii
let clean_ascii: String = my_str
    .filter(|c| c.is_ascii())
1 Like

The code example you give contains a few syntactic errors, and you could also do a better job providing the full error message. It’d be easiest to help you if you provided the full error message together with a complete code example so that we can reproduce that error message ourselves. I’m also not quite sure if what you want to achieve is entirely in line with the kind of code you’ve written here—not that trim_matches only removes parts of the string that are in the beginning or the end of the string, not in the middle.

Anyways, the argument to trim_matches can be a function from char to bool, you’re trying to combine two such functions here char::is_alphabetic and char::is_numeric and you can’t do that with & the way you’re trying to do. You’d need to create a new function, or a closure here, also you probably want a disjunction (logical OR-operation) instead of a conjunction (logical AND-operation), because AFAIK no character would be both alphabetic and numeric. Try something like

s.trim_matches(|c| c.is_alphabetic() || c.is_numeric());

The expression |c| c.is_alphabetic() || c.is_numeric() is a closure that takes an argument c (which is inferret to be of type char) and returns a boolean. The inner expression c.is_alphabetic() || c.is_numeric() is of type bool and uses the argument c. It’s using method-call syntax and is equivalent to something like char::is_alphabetic(c) || char::is_numeric(c). The || operator is a logical “OR” operation.

1 Like

Seems to me that trim_matches! takes a function as a parameter. fn(char) -> bool.

What you have passed in there is the & of two functions. At which point I get lost as to what happens but & produces a bool. Which is not a function.

1 Like

Okay, I'm only didn't understand how to delete it and wrote something, anyway thanks much.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.