英文:
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 < 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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论