[英]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.