string-manipulation

C# - String.Replace() but replace only the first match

Closed as a duplicate of How do I replace a string in .NET? I need to replace a sequence in a string, but only once. Part 1: What is the easiest way to do this in C#? Part 2: What is the most efficient way to do this in C#? Bonus points if they're both the same :D (not really, but I'll give you a cookie) ...

Memory Efficiency and Performance of String.Replace .NET Framework

string str1 = "12345ABC...\\...ABC100000"; // Hypothetically huge string of 100000 + Unicode Chars str1 = str1.Replace("1", string.Empty); str1 = str1.Replace("22", string.Empty); str1 = str1.Replace("656", string.Empty); str1 = str1.Replace("77ABC", string.Empty); // ... this replace anti-pattern might happen with upto 50 cons...

C# How do I extract each folder name from a path?

Hi all, My path is "\server\folderName1\another name\something\another folder\" How do I extract each folder name into a string if I don't know how many folders there are in the path and I don't know the folder names? Many thanks ...

Show a list of words repeated in haskell

I need to be able to write a function that shows repeated words from a string and return a list of strings in order of its occurrence and ignore non-letters e.g at hugs prompt repetitions :: String -> [String] repetitions > "My bag is is action packed packed." output> ["is","packed"] repetitions > "My name name name is Sean ." output...

Writing a string to a file causes an exception in C#

I'm getting a "FormatException: Input string was not in a correct format" error that I don't understand. I'm using the following lines to write a string to a text file: using (StreamWriter sw = new StreamWriter(myfilename, false, System.Text.Encoding.GetEncoding(enc))) { sw.Write(mystring, Environment.NewLine); } (the encoding p...

[bash] Escape a string for sed search pattern

In my bash script I have an external (received from user) string, which I should use in sed pattern. REPLACE="<funny characters here>" sed "s/KEYWORD/$REPLACE/g" How can I escape the $REPLACE string so it would be safely accepted by sed as a literal replacement? NOTE: The KEYWORD is a dumb substring with no matches etc. It is not sup...

Simple way to extract part of a file path?

I'm not very good at C, and I always get stuck on simple string manipulation tasks (that's why I love Perl!). I have a string that contains a file path like "/Volumes/Media/Music/Arcade Fire/Black Mirror.aac". I need to extract the drive name ("Media" or preferably "/Volumes/Media") from that path. Any help would be greatly appreciate...

Best way to repeat a character in C#

Hi, What it's the best way to generate a string of \t's in C# I am learning C# and experimenting with different ways of saying the same thing. Tabs(uint t) is a function that returns a string with t amount of \t's For example Tabs(3) returns "\t\t\t" Which of these three ways of implementing Tabs(uint numTabs) is best? Of course ...

Coalesce vs empty string concatenation

My coworker is new to C# and didn't know about the coalesce operator. So, I saw him write a line of code like this: string foo = "" + str; The idea being that if str is null, this expression would return an empty string. Of course, that could be rewritten as this: string foo = str ?? ""; And I feel that would be more readable. B...

Best way to strip punctuation from a string

For the hope-to-have-an-answer-in-30-seconds part of this question, I'm specifically looking for C# But in the general case, what's the best way to strip punctuation in any language? I should add: Ideally, the solutions won't require you to enumerate all the possible punctuation marks. Related: Strip Punctuation in Python ...

Use RegExp to match a parenthetical number then increment it

I've been trying to find a way to match a number in a Javascript string that is surrounded by parenthesis at the end of the string, then increment it. Say I have a string: var name = "Item Name (4)"; I need a RegExp to match the (4) part, and then I need to increment the 4 then put it back into the string. This is the regex I have s...

Built-In Character Casing functions in .Net

Are there any built-in functions in .Net that allow to capitalize strings, or handling proper casing? I know there are some somewhere in the Microsoft.VB namespace, but I want to avoid those if possible. I'm aware of functions like string.ToUpper and string.ToLower() functions however it affects the entire string. I am looking to someth...

Comparing Strings in Ruby

I need to take two strings, compare them, and print the difference between them. So say I have: teamOne = "Billy, Frankie, Stevie, John" teamTwo = "Billy, Frankie, Stevie" $ teamOne.eql? teamTwo => false I want to say "If the two strings are not equal, print whatever it is that is different between them. In this case, I'm just look...

How do I pass a String into a function in an NVelocity Template?

I'm using the NVelocity Templating engine to produce a fixed-length field output - you know the kind of thing: Field Start Pos Field Length Notes ---------- --------- ------------ --------- Supplier 1 7 Leading Zeros GRN 8 9 - ... e.g. >0001234 123A< The probl...

How to split strings into text and number?

I'd like to split strings like these 'foofo21' 'bar432' 'foobar12345' into ['foofo', '21'] ['bar', '432'] ['foobar', '12345'] Does somebody know an easy and simple way to do this in python? ...

automatic incrementing filename

I have a filename that has the following format: timestamp-username-1 This file is constantly etting written to, but before it gets too large I want to create a new file. timestamp-username-2 How can I acheive this with using least amount of memory (ie, no or little variables) here is my version: private void Split() { ...

Is this a bug in string.TrimEnd?

"\u4000\f".TrimEnd(new char[0]) is equal to "\u4000". I am passing an empty array, so according to the MSDN documentation nothing should be removed and "\u4000\f" should be returned. Is there a reason for this behaviour? EDIT: Clarified expected behaviour EDIT: Apparently, this changed in 3.5, I was looking at the 2.0 documentation...

Greasemonkey: Text processing - What's the best way to find certain words in a website and have a function work on it?

I want greasemonkey to scan through a website and change certain words to something else. Is there a way to do it with regex or some other string processing function in javascript? Thanks for your help :) ...

How do I create a random alpha-numeric string in C++?

I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string. How do I do this in C++? ...

How do I create a strong password string in C++?

Would like to create a strong password in C++. Any suggestions? I assume it should use alpha (upper and lower), numeric, special characters. It would be good to be able to specify a minimum length. It would be great to avoid characters that are hard to visually distinguish like "O" and "O" It would be great to void all characters same,...