【问题标题】:How to remove item from string c# [closed]如何从字符串c#中删除项目[关闭]
【发布时间】:2020-11-01 04:33:49
【问题描述】:

我需要帮助来编写删除项目的代码。我的代码在这里

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class shop : MonoBehaviour
{
    public int money = 0;
    public Text moneytext;
    public Text inventory;

    public void additem(string item)
    {
        moneytext.text = money.ToString();
        inventory.text += "\n" + item;
    }
}

我想制作 deleteitem 我尝试这个但错误。谢谢你的帮助

public void deleteitem(string item)
{ moneytext.text = money.ToString();
inventory.text -= "\n" + item;}

【问题讨论】:

  • 你想用 deleteitem 实现什么?
  • 嗯,我明白了。试试Replace() 方法。您不能将- 与字符串一起使用。
  • 我想在单击删除按钮时删除我的项目列表

标签: c# unity3d


【解决方案1】:

与其将项目存储在字符串中,不如将它们存储在列表中,并在需要它们的字符串表示时将它们连接在一起。

将此添加到您的变量中以获取包含您的项目的列表:

List<string> items = new List<>();

您可以像这样在列表中添加和删除项目:

items.Add(item);
items.Remove(item);

当您需要列表的字符串表示形式时,可以使用:

String.Join("\n", items);

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 2017-04-21
    • 2021-03-17
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多