需要帮助从给定的字符串中获取子字符串

时间:2014-07-17 10:03:07

标签: c# string split

我在应用程序中有以下字符串

string s =。\ abc_xyz \ drff \ dsqa_license_db \ dgfx \ xsad_license_db.rfds-17343;

我需要上面字符串中的子字符串为。\ abc_xyz \ drff \

请帮助我做到这一点

感谢

1 个答案:

答案 0 :(得分:0)

string s = @".\abc_xyz\drff\dsqa_license_db\dgfx\xsad_license_db.rfds-17343";

  string[] splitChar = { @"\"};

 int yourCriteria = 3;

   string[]  splitString = s.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

  StringBuilder sb = new StringBuilder();

 for (int i = 0; i < yourCriteria; i++)
  {
     if (sb.Length == 0)
   sb.AppendFormat("{0}", splitString[i]);
   else
  sb.AppendFormat(@"\{0}", splitString[i]);

  }
    //now do whatever you want to with sb.ToString();