Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Manual

Collapse Whitespace#

Operations / Transform / Collapse Whitespace

  • Can Read

  • Cannot Write

Description#

Replaces every run of spaces, tabs and new lines with a single space and trims the result

Parameters#

Name Description
Text The text that is collapsed

Code#

using System;
using System.Text;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Collapse Whitespace")]
    [Category("Operations/Transform/Collapse Whitespace")]

    [Description("Replaces every run of spaces, tabs and new lines with a single space and trims the result")]
    [Image(typeof(IconClear), ColorTheme.Type.Yellow)]

    [Parameter("Text", "The text that is collapsed")]

    [Keywords("Space", "Tab", "New Line", "Clean", "Normalize", "Squeeze", "Text")]

    [Serializable]
    public class TypeStringCollapseWhitespace : TypeString
    {
        [SerializeField] private GetString m_Text = new GetString();

        [NonSerialized] private readonly StringBuilder m_Builder = new StringBuilder();

        public override string Get(Args args)
        {
            string text = this.m_Text.Get(args);
            if (string.IsNullOrEmpty(text)) return string.Empty;

            this.m_Builder.Clear();
            bool previousIsWhitespace = false;

            foreach (char character in text)
            {
                bool isWhitespace = char.IsWhiteSpace(character);
                if (isWhitespace)
                {
                    if (!previousIsWhitespace) this.m_Builder.Append(' ');
                }
                else
                {
                    this.m_Builder.Append(character);
                }

                previousIsWhitespace = isWhitespace;
            }

            return this.m_Builder.ToString().Trim();
        }

        public override string ToString()
        {
            return $"Collapse {this.m_Text}";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.