CSharp examples for System:String Strip
Remove a comment in a string
using System.Globalization; using System.Collections; using System.Text; using System;/*w ww . j a v a 2 s. c o m*/ public class Main{ /// <summary> /// Remove a comment in a string /// </summary> public static string RemoveComment (string bufferToManage, string commentStartTag) { if (bufferToManage == null) throw new NolmeArgumentNullException (); if (commentStartTag == null) throw new NolmeArgumentNullException (); string ResultBuffer, ResultBufferTrim; int QuoteStart, QuoteEnd, iQuote; int iCommentStart; // Check if a comment is inserted iCommentStart = bufferToManage.IndexOf (commentStartTag); if (iCommentStart == -1) { // No comment inside, return same string ResultBuffer = bufferToManage; } else { // A comment tag has been found but it may be inside a string QuoteStart = bufferToManage.IndexOf ("\""); if (QuoteStart == -1) { // Return from [0..iCommentStart[ // Comment is at iCommentStart with length bufferToManage.Length-iCommentStart ResultBuffer = bufferToManage.Substring (0, iCommentStart); } else { // Check before if quote start AFTER comment start if (iCommentStart < QuoteStart) { // Return from [0..iCommentStart[ // Comment is at iCommentStart with length bufferToManage.Length-iCommentStart ResultBuffer = bufferToManage.Substring (0, iCommentStart); } else { // String & comment both exists, check if last commentStartTag is after last quote so it's a real comment to end of line QuoteEnd = bufferToManage.IndexOf ("\"", QuoteStart+1); if (QuoteEnd == -1) { // [ERROR] String not well terminated, assume full string ResultBuffer = bufferToManage + "\""; } else { iQuote = bufferToManage.LastIndexOf ("\""); iCommentStart = bufferToManage.LastIndexOf (commentStartTag); if (iCommentStart > iQuote) { // Remove this comment ResultBuffer = bufferToManage.Substring (0, iCommentStart); } else { // comment tag is inside string, don't modify ResultBuffer = bufferToManage; } } } } } // Last stage, remove start spaces & stabs ResultBufferTrim = ResultBuffer.Trim (); return ResultBufferTrim; } }