Login / Register
▼
Binary Fortress
Binary Fortress Software
CheckCentral
ClipboardFusion
CloudShow
DisplayFusion
FileSeek
HashTools
LogFusion
Notepad Replacer
Online Base64 Decoder
Online Base64 Encoder
Online JSON Formatter
ShellSend
TrayStatus
VoiceBot
WallpaperFusion
Window Inspector
More Apps...
DisplayFusion
CheckCentral
CloudShow
ClipboardFusion
FileSeek
VoiceBot
WallpaperFusion
▼
Download
Download
Change Log
Download Beta
Beta Change Log
License (EULA)
▼
Features
Features
HotKeys
Macros
Triggers
Clipboard Syncing
Clipboard Manager
Languages
Free vs Pro
Purchase
Screenshots
Apps
▼
Online
Online
Recent Items
Pinned Items
▼
Help
Help Guide
FAQ
Discussions
Contact Us
Find My License
Mailing Address
Advanced Settings
Macros
Languages
Clipboard
Fusion
WARNING: You currently have Javascript disabled!
This website will not function correctly without Javascript enabled.
Title
Message
OK
Confirm
Yes
No
Remove Outer Whitespaces and Replace Inner Ones With a Space (without auto paste)
Return to ClipboardFusion Macros
Description
This script will trim whitespace from the start and end of the copied text, and replace any whitespace within the text with a single space.
Language
C# (.Net)
Minimum Version
5.7+
Created By
Danail Branekov34799
Contributors
-
Date Created
May 7, 2020
Date Last Modified
May 7, 2020
Macro Code
Copy
Select All
Toggle Line Wrapping
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 string[] allLines = text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries); // Filter out lines that only contain whitespaces List<string> nonEmptyLines = new List<string>(); foreach (string line in allLines) { //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; nonEmptyLines.Add(line); } for(int i = 0; i < nonEmptyLines.Count; i++) { string line = nonEmptyLines[i]; //add the trimmed line to the StringBuilder builder.Append(line.Trim()); // Append a space after each line, except after the last one if(i != allLines.Length) { builder.Append(" "); } } //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; } }
Copyright © 2007-2021 Binary Fortress Software
•
News
•
Discussions
•
FAQ
•
Support
•
Privacy
•
ToS
•
Get Binary Fortress Merch