Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

C#: Break Long Strings of Text That Don't Contain A Space

Long strings of text with no spaces breaks most tables since the browser doesn't know where to start a new line. This function breaks a string after a number of characters so that they can be properly displayed in tables

   1  
   2  // Put this at the top
   3  using System.Text.RegularExpressions;
   4  //
   5  
   6  public string BreakLongString(string SubjectString, int CharsToBreakAfter)
   7  {
   8  	string Pattern = "\\S{" + CharsToBreakAfter + ",}";
   9  	int Counter = 0;
  10  	bool IsMatching = Regex.IsMatch(SubjectString, Pattern);
  11  	while (IsMatching)
  12  	{
  13  
  14  		Counter++;
  15  		string MatchedString = Regex.Match(SubjectString, Pattern).Value;
  16  		SubjectString = SubjectString.Replace(MatchedString.Substring(0, (CharsToBreakAfter - 1)), MatchedString.Substring(0, (CharsToBreakAfter - 1)) + " ");
  17  
  18  		// Prevent endless loops
  19  		if (Counter > 20) break;
  20  
  21  		// Check if we still have long strings
  22  		IsMatching = Regex.IsMatch(SubjectString, Pattern);
  23  	}
  24  
  25  	return SubjectString;
  26  }
  27  

get line break type (mac, linux, win and binary file)

this code analyses the first 5*1024 bytes of data from a file and tells which break type it uses, or if it's a binary file...

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  type
   6    TBreakType = ( btNone, btWin, btMac, btLin, btBin );
   7  
   8  ---------
   9  
  10  function GetBreakType( const Filename: string; const MaxDataToRead: Cardinal = 5*1024 ): TBreakType;
  11  var
  12    FS: TFileStream;
  13    Buffer, BufferStart, BufferEnd: PChar;
  14  begin
  15    Result := btNone;
  16    if not FileExists( Filename ) then
  17      raise Exception.Create( 'GetBreakType: nome de arquivo inválido' );
  18    try
  19      FS := TFileStream.Create( Filename, fmOpenRead );
  20      GetMem( Buffer, MaxDataToRead+1 );
  21      BufferEnd := ( Buffer + FS.Read( Buffer^, MaxDataToRead ) );
  22      BufferStart := Buffer;
  23      BufferEnd^ := #0;
  24    except
  25      raise Exception.Create( 'GetBreakType: erro alocando memória.' );
  26    end;
  27    try
  28      while Buffer^ <> #0 do begin
  29        if Result = btNone then
  30          if Buffer^ = ASCII_CR then begin
  31            if (Buffer+1)^ = ASCII_LF then begin
  32              Result := btWin;
  33              Inc( Buffer );
  34            end
  35            else
  36              Result := btMac;
  37          end
  38          else if Buffer^ = ASCII_LF then
  39            Result := btLin;
  40        Inc( Buffer );
  41      end;
  42      if Buffer <> BufferEnd then
  43        Result := btBin;
  44    finally
  45      FreeMem( BufferStart, MaxDataToRead+1 );
  46      FS.Free;
  47    end;
  48  end;
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS