Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Remove Linebreaks from Text

Description
This macro removes any line breaks from the text that's currently on the clipboard. It does not auto-paste the results.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Nov 29, 2022
Date Last Modified
Dec 6, 2022

Macro Code

using System;
using System.Text;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
		//copy whatever is selected to the clipboard
		//this will allow this macro to be used more quickly
		//text = BFS.Clipboard.CopyText();
		
		//make sure all of the line breaks are the same type
		text = text.Replace(Environment.NewLine, "\n");
		text = text.Replace("\r", "\n");
		text = text.Replace("\0", "\n");
		
		//this will let us quickly build the string to return
		StringBuilder builder = new StringBuilder();
		
		//split the text up into its separate lines
		foreach(string line in text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries))
		{
			//this is probably redundant since we are removing empty entries
			//if the line variable is null or empty, continue to the next line
			if(string.IsNullOrEmpty(line))
				continue;
				
			//if the line is blank, ignore it
			if(IsLineOnlyWhiteSpace(line))
				continue;
				
			//add the line to the StringBuilder
			builder.Append(line + " ");
		}
		
		//paste the StringBuilder
		//BFS.Clipboard.PasteText(builder.ToString());
		
		//tell ClipboardFusion to put the StringBuilder on the clipboard
		return builder.ToString();
	}
	
	//this is a set of blank characters
	private static readonly Dictionary<char, int> WhiteSpaceChars = new Dictionary<char, int>
	{
		{' ', 0},
		{'\x0085', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{'\x200B', 0},
		{' ', 0},
		{'\xFEFF', 0},
		{'\v', 0},
		{'\f', 0},
		{'\x2028', 0},
		{'\x2029',  0},
	};
	
	//this function returns true if a line is composed of only whitespace or blank characters
	private static bool IsLineOnlyWhiteSpace(string line)
	{
		foreach(char c in line)
		{	
			if(!WhiteSpaceChars.ContainsKey(c))
				return false;
		}
		
		return true;
	}
}