小编use*_*838的帖子

最后一次出现字符时分割字符串的最佳方法?

假设我需要像这样拆分字符串:

输入字符串:"我的名字是Bond._James Bond!" 输出2个字符串:

  1. "我的名字是邦德"
  2. "_占士邦!"

我试过这个:

int lastDotIndex = inputString.LastIndexOf(".", System.StringComparison.Ordinal);
string firstPart = inputString.Remove(lastDotIndex);
string secondPart= inputString.Substring(lastDotIndex + 1, inputString.Length - firstPart.Length - 1);
Run Code Online (Sandbox Code Playgroud)

有人可以提出更优雅的方式吗?

c# string split

46
推荐指数
3
解决办法
5万
查看次数

如何从 foreach 中的其他元素列表填充数组?

我想从其他元素列表中填充我的类型数组。所有这些我想在 foreach 循环中完成。

是否可以?

现在是如何做到的:

// Temp list
List<Parameter> inputData = new List<Parameter>();

// going through collection from which i want to copy
foreach (var parameter in parametersWindow.Parameters)
{
    inputData.Add(new Parameter() { Name = parameter.Name, Value = parameter.Value });
}

// Convertation to array.
Parameter[] parametersToInput = inputData.ToArray();
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的:

Parameter[] parametersToInput = new Parameter[parametersWindow.Parameters.Count]

foreach (var param in parametersWindow.Parameters)
{
   // parametersToInput.add(new Parameter(parameter))
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我吗?

c# arrays list

3
推荐指数
1
解决办法
1万
查看次数

C#中的字符串和C++/CLI中的String ^ ...如何传递"null"?

我有一些C++库.我还得到了C++/CLI包装器,以便在C#代码中调用来自该库的方法.

假设我想在C#中使用一些调用,如下所示:

string date = MyWrapper.GetValue("SystemSettings", "BuildDate", null);
Run Code Online (Sandbox Code Playgroud)

它将在C++/CLI上调用next函数:

static String^ GetValue(String^ section, String^ key, String^ defaultValue)
Run Code Online (Sandbox Code Playgroud)

我的问题:我有下一个ArgumentNullException:

值不能为null.\ r \nParameter name:managedString.

所以...问题:我应该如何null正确传递?谢谢.

c# c++ string c++-cli argumentnullexception

3
推荐指数
1
解决办法
3906
查看次数

我应该如何为HashSet重写Equals和GetHashCode?

让我说我上课:

    public class Ident
    {
        public String Name { get; set; }
        public String SName { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

还有一个:

    class IdenNode
    {
        public Ident id { get; set; }
        public List<IdenNode> Nodes { get; set; }

        public IdenNode()
        {
            Nodes = new List<IdenNode>();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想HashSet<IdenNode>大胆地使用它的两个元素是相同的(Equal)当且仅当它们的id.Names是Equal时.

所以,我要覆盖EqualsGetHashCode喜欢下一个:

        public override bool Equals(object obj)
        {
            IdenNode otherNode = obj as IdenNode;

            return otherNode != null && 
                   otherNode.id != null && 
                   id.Name == otherNode.id.Name;
        } …
Run Code Online (Sandbox Code Playgroud)

c# hash equals hashset

1
推荐指数
2
解决办法
1009
查看次数

我应该包括什么来抛出bad_function_call异常?

主题中的问题.我试过这个:

throw new std::bad_function_call("!");
Run Code Online (Sandbox Code Playgroud)

得到下一个错误:

错误C2039:'bad_function_call':不是'std'的成员

错误C2061:语法错误:标识符'bad_function_call'

c++ exception std

-6
推荐指数
1
解决办法
858
查看次数

标签 统计

c# ×4

c++ ×2

string ×2

argumentnullexception ×1

arrays ×1

c++-cli ×1

equals ×1

exception ×1

hash ×1

hashset ×1

list ×1

split ×1

std ×1