小编Jsc*_*mer的帖子

给定需要添加的空格数量,通过在单词之间添加空格来格式化字符串

给定一个起始字符串,并且需要在字符串中添加许多空格,是否有一种简单的方法?如果空格数分布不均匀,请从左至右添加空格。

这是我尝试过的:

将空格数除以实际的字符串空格

div = spaces // (word_count - 1)    
Run Code Online (Sandbox Code Playgroud)

标记化字符串

temp = st.split() 

for i in range(len(temp)):
Run Code Online (Sandbox Code Playgroud)

如果第一个单词只是添加当前单词而没有空格

if i == 0:

st = temp[0]
Run Code Online (Sandbox Code Playgroud)

如果第一个单词只是添加当前单词而没有空格

else: 
Run Code Online (Sandbox Code Playgroud)

在单词中添加div数量的空格+原始空格

st = st + " "*div + " " +temp[i]
Run Code Online (Sandbox Code Playgroud)

更新我们的空间计数

space_count = space_count - div
Run Code Online (Sandbox Code Playgroud)

space_count匹配或小于字符串中的实际空格量

if space_count <= word_count -1: 



st = st.replace(" ", "  ", space_count) 
Run Code Online (Sandbox Code Playgroud)

添加一个额外的空间,即“ space_count”次。这就是问题所在,它仅替换第一个space_count个空格字符。关于如何添加最后一个空格或找到更好的方法的任何提示?

这是一个例子:

“ Algernon。你听到我在玩什么吗,Lane?”

并给定space_Count = 12,应给出

Algernon.   Did   you   hear   what  I  was  playing,  Lane?
Run Code Online (Sandbox Code Playgroud)

编辑:工作正常!

python python-3.x

5
推荐指数
1
解决办法
83
查看次数

如何为我的项目目录中的两个不同文件夹运行一个“npm start”

我想运行一个npm start命令来运行我的前端文件夹和后端文件夹。目前,我必须在每个文件夹内导航并分别在两个文件夹上运行命令以查看我的应用程序在本地主机上打开。我已经“同时”查看了包,但是在我的 package.json 文件中实现它时遇到了一些问题。这是我的前端文件夹的 package.json 文件:

"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "react-snap"
Run Code Online (Sandbox Code Playgroud)

}

在前端运行 npm start 后,我​​尝试使用此位运行后端,但它不起作用:

"start": "set HOST = 'http://localhost' && set PORT=8000 && ../backend/ start & react-scripts start"
Run Code Online (Sandbox Code Playgroud)

编辑:重试后仍有一些麻烦。

这是我的 package.json 文件中的脚本

前端文件夹

"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject",
"postbuild": "react-snap" …
Run Code Online (Sandbox Code Playgroud)

node.js npm reactjs react-scripts

3
推荐指数
2
解决办法
6082
查看次数

使用 this.props.history.push('/') 的用户注销重定向不会重定向到所需页面

用户注销后,我希望页面导航回我的主屏幕,'/'. this.props.history.push('/')然而,我正在尝试使用,该页面保留在当前的个人资料页面上,/profile#signout并读取错误Cannot read property 'props' of undefined

这是我的 logOutUser() 函数的代码。我正在使用 Firebase 进行注销。

logOutUser(){
  var tempname = login.getUser();
  firebase
  .auth()
  .signOut()
  .then(function() {
    alert("Goodbye " + tempname + "!");
    this.props.history.push('/');
  })
  .catch(function(error) {
    alert(error.message);
  });
}
Run Code Online (Sandbox Code Playgroud)

这是由我的 navClicked() 函数调用的。此函数检查用户当前所在的导航部分,并将所需信息返回到渲染部分:

navClicked(name) {
     if(name === "signout"){
       return(
         <button className="btn btn-info" onClick={this.logOutUser()}>
           Sign Outed
         </button>
       )
     }
 }
Run Code Online (Sandbox Code Playgroud)

最后这是我的渲染部分。我正在使用 react-bootstrap 卡并检查在配置文件卡上单击了哪个导航。

render(){
        const length = window.location.href.length;
        const current = window.location.href.slice(30, length);
        return(
        <Card className = "cardosettings" style={{ …
Run Code Online (Sandbox Code Playgroud)

firebase reactjs

2
推荐指数
1
解决办法
546
查看次数

标签 统计

reactjs ×2

firebase ×1

node.js ×1

npm ×1

python ×1

python-3.x ×1

react-scripts ×1