我正在尝试提出一个能够接收字符串并将其拆分为n个字符的消息的代码。每条消息不能超过n个字符,但不能超过n个字符,因为单词不应拆分。例如,“这是示例消息”,n = 10。此代码将返回 '这是一' '例' “消息”
关于如何解决这个问题的任何建议?
const searchingFor = (term) => {
return (x) => {
return x.tecnologia.toLowerCase().includes(term.toLowerCase()) || !term;
}
}
class FilterClass extends React.Component {
constructor(props) {
super(props);
this.state = {
empresas: [
{
empresa: "Daniel",
tecnologia: "React",
clientes: ["sodimac", "ripley", "paris"],
experiencia: "5 años",
id: 1
},
{
empresa: "Juan",
tecnologia: "React",
clientes: ["Falabella", "ripley", "ikea"],
experiencia: "3 años",
id: 2
},
{
empresa: "David",
tecnologia: "Angular",
clientes: ["Falabella", "ripley", "ikea"],
experiencia: "3 años",
id: 2
},
{
empresa: "Carlos",
tecnologia: "Vue",
clientes: ["Copec", "Wallmark", "Unimark"],
experiencia: "6 años",
id: 3
}
],
term: ''
}
this.searchHandler = this.searchHandler.bind(this);
}
searchHandler(event) {
this.setState({ term: event.target.value })
}
render() {
const {term, empresas} = this.state;
return (
<div>
<form className="container--flex-center">
<input type="text" onChange={this.searchHandler} value={term} />
</form>
{empresas.filter(searchingFor(term)).map(item =>
<FilterComponent
empresa={item.empresa}
tecnologia={item.tecnologia}
clientes={item.clientes}
experiencia={item.experiencia}
key={item.id}
/>
)}
</div>
)
}
}
export default FilterClass;
x是我认为需要的消息数。有人可以向我解释如何完成我的代码吗?
答案 0 :(得分:1)
这就是textwrap库的作用。
>>> from textwrap import wrap
>>> print(wrap('This is an example message', 10))
['This is an', 'example', 'message']