小编Jim*_*hel的帖子

循环遍历字符串以查找子字符串

我有这个字符串:

text = "book//title//page/section/para";
Run Code Online (Sandbox Code Playgroud)

我想通过它来查找所有//和/和它们的索引.

我尝试这样做:

if (text.Contains("//"))
{
    Console.WriteLine(" // index:  {0}  ", text.IndexOf("//"));   
}
if (text.Contains("/"))
{
    Console.WriteLine("/ index:  {0}  :", text.IndexOf("/"));    
}
Run Code Online (Sandbox Code Playgroud)

我也在考虑使用:

Foreach(char c in text)
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为//不是一个单一的char.

我怎样才能实现我的目标?

我也尝试了这个,但没有显示结果

string input = "book//title//page/section/para"; 
string pattern = @"\/\//";


Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
 if (matches.Count > 0)
  {
      Console.WriteLine("{0} ({1} matches):", input, matches.Count);
      foreach (Match match in matches)
         Console.WriteLine("   " + input.IndexOf(match.Value));
 }
Run Code Online (Sandbox Code Playgroud)

先感谢您.

c# string

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

如何遍历KeyValuePair的集合

我遍历KeyValuePair的集合,然后将Key和Value复制到一个新创建的类中,如下所示:

     Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
        List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();

        foreach (KeyValuePair<string, string[]> acct in acctData)
        {
            membHier.entityName = acct.Key;
            membHier.Accounts = acct.Value;
            membHierList.Add(membHier);                
        }
Run Code Online (Sandbox Code Playgroud)

问题在于,在第二次迭代中,membHierList属性立即被第一次迭代中的值覆盖。真奇怪

因此,在第一次迭代时,membHier.entityName是“ ABC Member”,并且使用字符串数组填充Accounts没问题。

然后在第二次迭代中,membHier.entityName为“ XYZ成员”。

现在,“ XYZ成员”占据了两个插槽,如下所示

membHierList [0] .base.entityName =“ XYZ成员” membHierList [1] .base.entityName =“ XYZ成员”

我上方有物体冲突吗?

预先谢谢你。

c#-4.0 keyvaluepair

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

存储概率分布而不保存单个值

我正在计算操作期间的许多(~1亿)浮点值.我不想将它们全部存储在内存中,但我想保存集合的粗略分布.

我的想法是确定所有值的指数并在直方图中计算它们.但是,这当然只有在值具有不同的指数时才有效.

有谁知道如何在不知道分布如何的情况下做到这一点?

memory algorithm math distribution probability

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

如何让IndexOf()方法返回正确的值?

我一直在使用googlemaps,我现在正在寻找格式坐标.

我得到以下格式的坐标:

地址(坐标)缩放级别.

我使用indexof方法得到"("+1的开头,这样我得到坐标的第一个数字,并将这个值存储在我称之为"start"的变量中.

然后我做同样的事情,但这次我得到索引")"-2得到最后一个坐标的最后一个数字,并将这个值存储在我称之为"结束"的变量中.

我收到以下错误:"索引和长度必须引用string.Parameter name中的位置:length"

我得到以下字符串作为一个imparameter:

"Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5"
Run Code Online (Sandbox Code Playgroud)

根据我的计算,我应该在start变量中得到值36,在end变量中得到值65

但出于某种原因,我在开始时得到41,在结束时得到71.

为什么?

public string RemoveParantheses(string coord)
        {
            int start = coord.IndexOf("(")+1;
            int end = coord.IndexOf(")")-2;

            string formated = coord.Substring(start,end);
            return formated;
        }
Run Code Online (Sandbox Code Playgroud)

然后我尝试硬编码正确的值

string Test = cord.Substring(36,65);
Run Code Online (Sandbox Code Playgroud)

然后我得到以下错误:

startindex不能大于字符串的长度.参数名称startindex

我理解这两个错误的意思,但在这种情况下,它们是不正确的,因为我不会超出字符串长度值.

谢谢!

.net c# asp.net

0
推荐指数
1
解决办法
991
查看次数

为什么我不能在Parallel.For或Parallel.Foreach上使用List.Add()?

这是我的代码:

Parallel.ForEach(Students, item =>
{
   StudentModel studentModel = new StudentModel(item);

   // Maybe he/she has alot of name
   foreach (var words in studentModel.StudentNames.Split(','))
   {
        if (string.IsNullOrWhiteSpace(words))
                return;

        string tempWords = words.Trim();
        // add it to student names list
        STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);


     }
     // add it to student list
     STUDENT_MODELS.Add(studentModel);
});
Run Code Online (Sandbox Code Playgroud)

我想做的是,我得到了学生名单.将其转换为学生模型,获取学生姓名(因为一个学生有很多名字),然后我将名字添加到名单,这是因为可能以后我需要得到同名学生并做一些事情......最后添加学生到学生模特名单.

问题发生在:

STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);
Run Code Online (Sandbox Code Playgroud)

这个地方总是发生:System.IndexOutOfRangeException

我已经将Paralle.Foreach更改为Parallel.For,并将foreach更改为for,但没有任何更改.我必须使用Parallel,因为学生数量大约为100000,如果我只使用foreach替换Parallel.Foreach,则需要160+秒,如果我锁定那个地方.....还是慢......如果使用Parallel. Foreach,它将使用20秒左右,但我无法处理异常.

我已经尝试用这个替换它:

StudentNames name = STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault();
if (null != name)
   name.student.Add(StudentModel);
Run Code Online (Sandbox Code Playgroud)

但问题仍然发生在某些时候........如果我只是尝试...抓住并忽略它,然后当我以后访问STUDENT_NAMES列表时,它仍然抛出异常....... .....

我也尝试使用ConcurrentBag …

.net c# linq plinq

0
推荐指数
1
解决办法
648
查看次数

在java中存储10,000条记录的最佳数据结构

我在面试时被问到这个问题.我认为这个问题过于通用,无法指定特定的数据结构.

但是,如果我们将问题通道化为以下标准,那么使用的最佳数据结构是什么:

  1. 如果插入速度应该最快?
  2. 如果要搜索特定数据最快?

java algorithm data-structures

0
推荐指数
1
解决办法
977
查看次数

调整堆大小时 C 中的 realloc 错误

我想在堆的插入函数中使用 C 中的 realloc。这是代码:

\n
typedef struct MaxHeap {\n    int size;\n    int* heap;\n} MaxHeap;\n\nvoid max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);\n    max_heap[max_heap->size] = N_INF;\n    max_heap->size += 1;\n    increase_key(max_heap, key_index, max_heap->size, key)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到这个警告:

\n

warning: passing argument 1 of \xe2\x80\x98realloc\xe2\x80\x99 makes pointer from integer without a cast [-Wint-conversion]我尝试了这个修复:

\n
max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));\n\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

我这样做了:

\n
void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n    int* …
Run Code Online (Sandbox Code Playgroud)

c heap

0
推荐指数
1
解决办法
86
查看次数

静态类与仅使用静态方法的类之间的区别是什么?

静态类与仅使用静态方法(例如私有构造函数)的类有什么区别?

我们有:

using System;

public class Test
{
        public static void Main()
        {
                Info.SetName("nnn");
        Console.WriteLine(Info.Name);
        Info.SetName("nn2");
        Console.WriteLine(Info.Name);

        Info2.SetName("nnn");
        Console.WriteLine(Info2.Name);
        Info2.SetName("nn2");
        Console.WriteLine(Info2.Name);
        }
}

public class Info
{
    public static string Name;
    public static void SetName(string name){
        Name = name;
    }
}

public static class Info2
{
    public static string Name;
    public static void SetName(string name){
        Name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么从方法/属性访问性能,代码可读性,代码可扩展性的角度来看?

.net c# oop class

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