简体   繁体   English

我如何将两个子字符串绑定到一个?

[英]How i can bind two substring to one?

I have two string 我有两个弦

private StringProperties firstName;
private StringProperties lastName;
private StringProperties nickName;

the first and last name are picked by user, the nickName is a concatenation of first 3 character of first and lastname 用户选择名字和姓氏,昵称是名字和姓氏的前3个字符的串联

How i can do that? 我该怎么做?

Actually i initialize it like that (this is the entire class). 实际上,我这样初始化它(这是整个类)。

public class Person {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty nickName;
private ObservableList<Evento> eventi = FXCollections.observableArrayList();

public Person(String firstName, String lastName) {
    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);
    if (firstName.length() > 2 && lastName.length() > 2)
        this.nickName = new SimpleStringProperty(firstName.trim().substring(0,3).concat(lastName.trim().substring(0,3)));
    else
        this.nickName = new SimpleStringProperty("");
}

public ObservableList<Evento> getEventi() {
    return eventi;
}

public String getFirstName() {
    if(firstName == null) firstName = new SimpleStringProperty(this,"firstName");
    return firstName.get();
}

public StringProperty firstNameProperty() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName.set(firstName);
}

public String getLastName() {
    if(lastName == null) lastName = new SimpleStringProperty(this, "lastName");
    return lastName.get();
}

public StringProperty lastNameProperty() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName.set(lastName);
}

public String getNickName() {
    if(nickName == null) nickName = new SimpleStringProperty(this,"nickName");
    return nickName.get();
}

public StringProperty nickNameProperty() {
    return nickName;
}

@Override
public String toString() {
    return getNickName() + "(" + getLastName() + " " + getFirstName() + ")";
}

} }

but when i let the user change first or lastName, the nickName won't update. 但是当我让用户更改名字或姓氏时,昵称不会更新。

You should use ReadOnlyStringProperty for the nickname: 您应该使用ReadOnlyStringProperty作为昵称:

private ReadOnlyStringWrapper nickName= new ReadOnlyStringWrapper();
...
public final String getNickName() {
    return nickName.get();
}

public final ReadOnlyStringProperty nickNameProperty() {
    return nickName.getReadOnlyProperty();
}

As for binding, you can use utility methods from Bindings class or implement your own binding for any other complicated cases. 至于绑定,您可以使用Bindings类中的实用程序方法,也可以针对任何其他复杂情况实现自己的绑定。 This example uses createStringBinding() method. 本示例使用createStringBinding()方法。 It takes Callable functional interface, which will be used to calculate new value, and list of observable properties, which values will be observed for changes: 它具有Callable功能接口(将用于计算新值)和可观察属性的列表(可观察到的值变化):

public Person(String firstName, String lastName) {
    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);

    this.nickName.bind(Bindings.createStringBinding(()->{
        if(this.firstName.get().length() > 2 && this.lastName.get().length() > 2) {
            return this.firstName.get().substring(0,3).concat(this.lastName.get().trim().substring(0,3));
        } else {
            return "";
        }
    }, this.firstName, this.lastName));

}

You can use Bindings.format : 您可以使用Bindings.format

nickName.bind(Bindings.format("%.3s%.3s", firstName, lastName));

The 3 in %.3s is the maximum length of the string. %.3s3是字符串的最大长度。


This won't do any trimming of the strings though, (you could do that before passing the strings to firstName and lastName ). 但是,这不会对字符串进行任何修剪(您可以在将字符串传递给firstNamelastName之前进行此修剪)。

It will also work on strings that are smaller than 3 characters. 它也适用于小于3个字符的字符串。 So, you can get nicknames like FoBar , FooB or Bar (if the first name is an empty string). 因此,您可以获得昵称,例如FoBarFooBBar (如果名字是一个空字符串)。

暂无
暂无

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

相关问题 如何替换仅出现一次的子字符串,而连续两次或多次排除子字符串呢? - how can I replace substring that occurs only once while excluding substring two or more times in a row? 如何选择由原始字符串表示的子字符串(除了两个初始字符之外)? - How can I select the substring represented by the original string except the two initial characters? 如何在 function 中的 Substring 和比较器中的两个值中实现二进制搜索? - How can I implement binary search in Substring in function and two values in Comparator? 我如何将两个字符串的编译中的 substring 个字符等于最大长度 35 - how can i substring character from compilation of two string to be equal to max length 35 如何在两个单独的程序上将两个套接字绑定到同一端口? - How can I bind two sockets to the same port on two separate programs? 如何忽略子字符串中的空格? - How can I ignore spaces in a substring? Java如何删除字符串中两个子字符串之间的所有内容 - Java how can remove everything between two substring in a string 如何通过指数关系将两个SimpleDoubleProperty相互绑定? JavaFX - How can I bind two SimpleDoubleProperty`s to each other with an exponential relation? JavaFX 我可以将这两者合二为一吗? - Can I combine these two into one? 如何使用Google Guice将两个类别绑定为一个类别? - How to bind two classes into one classes using google guice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM