如何在将两个字符串相加时生成相同的结果,即 ‘A+B’ == ‘B+A’。

huangapple go评论77阅读模式
英文:

How to generate the same result when adding two Strings 'A+B' == 'B+A'

问题

我正在制作一个Flutter聊天应用程序。在我的应用程序中,当有人创建账号时,他们将拥有一个用户名(username)和用户ID(userId)以及其他属性的字符串值。

我遇到的问题是,当用户'A'向用户'B'发送消息时,它会创建一个名为'A_B'的通道,我是通过A.username + B.username来实现的,但是当用户'B'向用户'A'发送消息时,如何确保它会创建相同的通道名称,而不是'B_A'呢?

英文:

I am making a Flutter chat app. In my app, when one creates an account, they will have a string value of username and userId along with other attributes.

The problem I am having is that, when user 'A' send a message to user 'B', it creates a channel called 'A_B', which I did by A.username + B.username, but how do I make sure that, when user 'B' sends message to user 'A', it will create the same channel name and not 'B_A'?

答案1

得分: 3

数学中这被称为规范形式。建立一个惯例,即始终用字母顺序排列的用户名来表示你的频道名称。就像这样:

if A < B:
    channel = A + B
else:
    channel = B + A

是的,你希望使用在用户名中无效的字符来将它们组合起来,以避免像 "AB" 和 "C" 与 "A" 和 "BC" 这样的名称产生歧义。

英文:

In math this would be called a canonical form. Establish a convention that your channel name is always represented by the usernames in alphabetical order. Something like this:

if A &lt; B
    channel = A + B
else
    channel = B + A

And yes, you want to combine them with a character(s) that aren't valid in the usernames to avoid ambiguity with names like "AB" and "C" vs "A" and "BC".

huangapple
  • 本文由 发表于 2020年10月24日 04:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64506692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定