小编Den*_*ena的帖子

将字符串数组分配给列表<myclass>

我运行此代码时遇到了Stackoverflow

class Students
{
    public int SID { get { return SID; } set { SID = value; } }
    public string SName { get { return SName; } set { SName = value; } }      
}
Run Code Online (Sandbox Code Playgroud)

问题位于foreach(名称中的字符串)..我无法提前将字符串数组存储到我的数据结构中

 class Program
 {
     static void Main(string[] args)
     {
         List<Students> sList = new List<Students>();           
         string[] names = new string[5]  {"Matt", "Joanne", "Robert"};
         System.Console.WriteLine("{0} words in text:", names.Length);

         foreach (string s in names)
         {
             Students st = new Students();
             st.SName = s;
             sList.Add(st);
             System.Console.WriteLine("test{0}",s); …
Run Code Online (Sandbox Code Playgroud)

c# arrays list

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

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

我有这个字符串:

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万
查看次数

标签 统计

c# ×2

arrays ×1

list ×1

string ×1