String Extensions

Universal CS Scripts - Scripts - String Extensions

How to add Spacing between lower and upper case characters with C#

To to add Spacing between lower and upper case characters i first loop though the string and get all upper case characters and add them to a list if the upper case character does not follow an upper character eg : ABCdef will out put ABC def.

public static string AddSpacesToSentence(this string text)

How to Split a String with another String with C#

To split a string with another string simply use the split method and pass in a new string array with the string to split.

public static string[] SplitWithString(this string text, string msg) => text.Split(new string[] { msg }, StringSplitOptions.None);

How to get split a String in to individual lines with C#

To get individual lines from a string first replace all carriage returns with /r with an empty string or "" then split the string with \n or Environment.NewLine and this will return an array for each line.

public static string[] GetLines(this string text) => text.Replace("\r", "").Split('\n');

How to file size as a readable string with C#

To get the file size first use FileInfo and give it the file path then get the length this will return as a long. then pass the long to the method BytesToString and this will return a readable string.

return new FileInfo(FilePath).Length.BytesToString();

eg :
1 B Bytes
1 KB Kilobyte
1 MB Megabyte
1 GB Gigabyte
1 TB Terabyte
1 PB Petabyte
1 EB Exabyte

How to reverse a string with C#

To reverse a string it is rather simple. Just use the methods ToCharArray then Reverse then finally ToArray().

public static string ReverseString(this string input) => new String(input.ToCharArray().Reverse().ToArray());

StringExtensions.cs 2.5KB


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

static class StringExtensions
{
    public static string AddSpacesToSentence(this string text)
    {
        List<char> chars = new List<char>();

        text = text.TrimStart().TrimEnd();

        for (int i = 1; i < text.Length - 1; i++)
        {
            if (i < text.Length - 1)
            {
                if (char.IsUpper(text[i]) && !char.IsUpper(text[i - 1]))
                {
                    if (!chars.Contains(text[i]))
                    {
                        chars.Add(text[i]);
                    }
                }
            }
        }

        string after = text;
        foreach (var C in chars)
        {
            after = after.Replace($"{C}", $" {C}");
        }
        return after;
    }
    static string BytesToString(this long byteCount)
    {
        string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
        if (byteCount == 0)
        {
            return "0" + suf[0];
        }
        long bytes = Math.Abs(byteCount);
        int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
        double num = Math.Round(bytes / Math.Pow(1024, place), 1);
        return (Math.Sign(byteCount) * num).ToString() + suf[place];
    }
    public static string GetFileSizeReadable(this string FilePath)
    {
        if (File.Exists(FilePath))
        {
            return new FileInfo(FilePath).Length.BytesToString();
        }
        return $"No File at path {FilePath}";
    }
    public static string GetLine(this string text, int lineNo)
    {
        string[] lines = text.GetLines();
        if (lines.Length - 1 < lineNo) { return ""; }
        return lines[lineNo];
    }
    public static string[] GetLines(this string text) => text.Replace("\r", "").Split('\n');
    public static string HtmlOpenClose(this string text, string idenifier, params string[] args)
    {
        string x = $"";
        foreach (var item in args)
        {
            x += item;
        }
        return $"<{idenifier} {x}>{text}</{idenifier}>";
    }
    public static string ReverseString(this string input) => new String(input.ToCharArray().Reverse().ToArray());
    public static string SafeHtmlString(this string text) => System.Net.WebUtility.HtmlEncode(text);
    public static string[] SplitWithString(this string text, string msg) => text.Split(new string[] { msg }, StringSplitOptions.None);
}
Powered by w3.css