且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何修剪字符串并显示特定部分

更新时间:2023-10-31 18:39:10

使用正则表达式:

  -  +警告 -  + [^  - ] +  -  + 

应该这样做。


按照示例,一个简单的方法是搜索'Warnings'行,然后在结果字符串中包含所有剩余部分。

例如

  public   static   string  mytrim( string  ins)
{
string [] line = ins.Split( new char [] {' \ n'}); // 获取行数
int n;
for (n = 0 ; n< line.Length; ++ n)
{
if (line [n] .Contains( - 警告 - )) // 搜索警告行
break ;
}
StringBuilder sb = new StringBuilder();
for (; n < line.Length; ++ n) // 使用剩余的(可能为空的)部分来构建结果字符串
{
sb。追加(行[N]);
sb.Append( \ n);
}
return sb.ToString();
}


Hi all

Below is the string i am getting

"One or more warnings have occurred. It is recommended that you follow the
instructions provided to correct the problem then try again.
-----------------------------------Information----------------------------------
Control Station: Check if standby is up
Information HC_CS_27389984778: The standby Control Station is
	 currently powered on. It will be powered off during upgrade, and then
	 later restarted and upgraded.
--------------------------------------------------------------------------------
------------------------------------Warnings------------------------------------
Blades : Check virus checker server configuration
Warning HC_DM_18800115743:
	 * vnxdm01: The virus checker configuration contains only one Virus
	   Checker (VC) server. High availability is compromised. It is
	   recommended to add at least a new VC server.
Action : Use the EMC Celerra AntiVirus MMC to add a new VC server
	 in the virus checker configuration, or edit the configuration
	 file manually.
--------------------------------------------------------------------------------"



i want this

------------------------------------Warnings------------------------------------
Blades : Check virus checker server configuration
Warning HC_DM_18800115743:
	 * vnxdm01: The virus checker configuration contains only one Virus
	   Checker (VC) server. High availability is compromised. It is
	   recommended to add at least a new VC server.
Action : Use the EMC Celerra AntiVirus MMC to add a new VC server
	 in the virus checker configuration, or edit the configuration
	 file manually.
--------------------------------------------------------------------------------"



Please let me know how to do this

Use a regex:
-+Warnings-+[^-]+-+

Should do it.


Following the example, a simple approach would be searching for the 'Warnings' line and then include in your result string all the remaining part.
e.g.
public static string mytrim(string ins)
{
  string[] line = ins.Split(new char[] { '\n' }); // obtain an array of lines
  int n;
  for (n=0; n<line.Length; ++n)
  {
     if (line[n].Contains("-Warnings-")) // search for the 'Warnings' line
       break;
  }
  StringBuilder sb = new StringBuilder();
  for (; n < line.Length; ++n) // use the remaining (possibly empty) part for building the result string
  {
     sb.Append(line[n]);
     sb.Append("\n");
  }
  return sb.ToString();
}