简体   繁体   English

当原始字符串是姓氏,名字时,如何在文本字段中显示名字? 反应/Spfx

[英]How to display firstname in a textfield when original string is surname, firstname? React/Spfx

I want to simple extract the firstname from a string.我想简单地从字符串中提取名字。 The Firstname and lastname is separated by a comma.名字和姓氏用逗号分隔。

I understand it will involve an inbuilt function to find the comma, then extract all the string AFTER the comma.我知道它将涉及一个内置函数来查找逗号,然后在逗号之后提取所有字符串。

I want it to be displayed in a text field.我希望它显示在文本字段中。

 public _getUser() { //getUser sets the state for multiple properties of the user. 

    sp.web.currentUser.get().then((user) => {

      this.setState({
        CurrentUserTitle: user.Title,
        ExtractedFirstName: 

The code above is what I'm using to get the logged in User.上面的代码是我用来获取登录用户的代码。 I imagine a function will have to be created but what would I put in the function, to then set to state?我想必须创建一个函数,但是我会在函数中放入什么,然后设置为状态?

If the first name and last name are in the following shape: firstName,lastName , you can just split the string on a comma and extract the first part of the resulting array:如果名字和姓氏的形状如下: firstName,lastName ,您只需将字符串拆分为逗号并提取结果数组的第一部分:

this.setState({
  // ...
  ExtractedFirstName: firstNameAndLastNameCombined.split(',')[0]

If the combination of first name and last name has some whitespace, you can also trim the result:如果名字和姓氏的组合有一些空格,您还可以修剪结果:

this.setState({
  // ...
  ExtractedFirstName: firstNameAndLastNameCombined.split(',')[0].trim()

Here's an example for both:以下是两者的示例:

 const firstNameAndLastNameCombined = 'Cool,Example'; console.log( firstNameAndLastNameCombined.split(',')[0] ) // Cool const firstNameAndLastNameCombinedWithSpace = ' Cool , Example '; console.log( firstNameAndLastNameCombinedWithSpace.split(',')[0].trim() ) // Cool

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串中删除不是名字/姓氏字符的字符 - Remove characters from a string that are not firstname/surname characters 我如何在反应中将名字/姓氏更改为名字/姓氏? 问题是它来自后端 - how can i change firstname/lastname to firstName/lastName in react? the problem is that it comes from the backend 我如何在 javascript user[0].firstName 中替换为 user.firstName - how do i replace in javascript user[0].firstName with user.firstName 我可以部分显示我的内容(显示名字而不是名字和姓氏) - I can partially display my content (displaying firstName instead of firstName and lastName) 如何在“名字”形式与我的函数之间建立联系? - How to connect between the form “firstname” to my function? 如何将 firstName 和 lastName 添加到 fullName? - How can I add firstName and lastName to fullName? JS/如何通过名字和姓氏输入进行搜索,但如果我在名字后键入空格也可以工作 - JS/ How to search with input by firstname and lastname but also work if i type space after firstname 如何使用变量编写此 RegExp /^\s*FirstName\s*$/? 我想用变量替换 FirstName? - How to write this RegExp /^\s*FirstName\s*$/ using a variable? I want to replace FirstName with a variable? 反应挂钩:TypeError:无法读取null的属性“ firstName” - React Hooks : TypeError: Cannot read property 'firstName' of null 在 react-table-2 的同一列中显示名字和姓氏 - Displaying firstname and last name in the same column in react-table-2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM