A string is a sequence of characters. It is commonly used to represent text or a sequence of bytes. Use this tag along with the appropriate programming language being used.

learn more… | top users | synonyms (1)

103
votes
16answers
16k views

Calculating entropy of a string

We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality. I whipped up this simple method which counts unique characters in a string, but it is quite ...
74
votes
14answers
12k views

Remove-Last-Comma-Problem

is there a more elegant way to solve this problem? ...
26
votes
3answers
2k views

Avoiding Python multiline string indentation

I want to make this code: ...
23
votes
5answers
1k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
21
votes
7answers
4k views

Finding words of different lengths given six letters

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
20
votes
6answers
6k views

Matching a generated string of random letters to an input

I made a program in Python and wanted it to be faster, so I wrote it on C# because it's compiled. To my surprise, the Python program is much faster. I guess there is something wrong with my C# code, ...
19
votes
5answers
2k views

Custom indexOf() without String methods

I created my own indexOf function. I was wondering if anyone could help me come up with a way to make it more efficient. I am practicing for interviews so the catch ...
18
votes
9answers
31k views

Converting between std::wstring and std::string

While researching ways to convert back and forth between std::wstring and std::string, I found this conversation on the MSDN ...
17
votes
9answers
9k views

Most efficient way in C++ to strip strings

If I want to strip a string completely of its whitespaces, punctuation and numbers (i.e. anything that is not A-Z, a-z), what is the most efficient way of doing it in C++? I tried this: ...
17
votes
4answers
10k views

Reverse a string word by word

Given an input string, reverse the string word by word. For example: Given s = "the sky is blue", return ...
17
votes
3answers
10k views

Reusing StringBuilder or creating a new one?

Given that it is better to reuse objects than create new ones when developing with Android, is it worth while deleting the contents of a StringBuilder and reusing ...
15
votes
7answers
2k views

Why does caching of string objects perform faster?

I'm trying to implement String-Pool. I'm maintaining a HashMap<String,String> to store the keys. ...
15
votes
8answers
15k views

Random string generation

I'm using this C# function to generate random coupons for a system. How can I improve it? ...
15
votes
7answers
9k views

Check for balanced parentheses

Given an expression string exp, write a program to examine whether the pairs and the orders of "{","}","(",")","[","]" are correct in exp. For ...
15
votes
6answers
2k views

Testing whether characters of a string are unique

Critiques (even pedantic ones) are welcome. ...
15
votes
4answers
936 views

Is there a better way to build my CSV output than String.Format()?

I'm generating CSV strings for various 3rd party utilities and this section of code gets repeated in many classes. Is there a better way to generate this string? ...
15
votes
4answers
172 views

Int extension for translating integer to plain English

This code was inspired by this question: Number to Words ...
15
votes
2answers
927 views

Using large amounts of if-else statements for playing card numbers

I have a method that uses a bunch of if-else statements, and I am thinking how I could simplify it. ...
15
votes
4answers
297 views

Name Filtering using a Trie

This question suggests a different (improved?) implementation for searching/filtering names based on a search query. This is based on the question here: Name filtering in Java A Trie allows the ...
14
votes
3answers
13k views

Reverse a String in Java

Here is the code for the CString class that I created. ...
14
votes
5answers
18k views

Count number of each char in a String

I know there is a simpler way of doing this, but I just really can't think of it right now. Can you please help me out? ...
14
votes
5answers
5k views

Reverse words in a given String

I'd like this code to be improved Input string: codereview is awesome Output string: awesome is codereview My approach: ...
14
votes
7answers
2k views

Determining whether two scrambled strings are equivalent

I was able to complete the problem doing it in this manner, but I would assume there has to be an easier way. Sample input: abc, cba Output: ...
14
votes
6answers
5k views

Function for removing forbidden characters

...
14
votes
5answers
2k views

Reversing the digits of an integer

For CS class, I had to reverse the digits of an integer from the input. This is my solution: ...
14
votes
2answers
849 views

The Breakfast Club attendance

This code checks a string called breakfastHours similar to 1567 which translates to Monday, Friday, Saturday, Sunday. ...
14
votes
2answers
513 views

Python-like C string library

Most of the C code I have written has never been seen by anybody else, and I wonder if my code follows normal practices, especially how my code performs security-wise. The code is for a library that I ...
14
votes
2answers
107 views

Playing with strings

I wanted to dust off my C skills, so I decided to write a couple string manipulation functions. I aimed for simplicity and safety. Performance was not a concern. Please be a thorough as you like. C ...
13
votes
1answer
1k views

Capitalize the first character of all words (even when following a '-')

I got it working, all variations are displayed Sint-Anna as should be, but I wonder, is there a simpler way to this, since it looks very cluttered? ...
13
votes
2answers
24k views

Integer to Alphabet string (“A”, “B”, …“Z”, “AA”, “AB”…)

So this question is prompted by two things. I found some code in our source control doing this sort of things. These SO questions: ...
13
votes
2answers
24k views

Pack and unpack bytes to strings

I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that ...
12
votes
7answers
3k views

Is this a fast implementation of reversing a string?

I thought of this algorithm today. It seems fast, but how can I know? ...
12
votes
4answers
843 views

Better way to manipulate this string in sequence?

I am working on a small custom Markup script in Java that converts a Markdown/Wiki style markup into HTML. The below works, but as I add more Markup I can see it becoming unwieldy and hard to ...
12
votes
7answers
2k views

Class for handling unit conversions

I am creating a class to be the end all defacto class for handling unit conversions. This is particularly difficult in my company's industry where imperial architectural strings are used as units. I ...
12
votes
10answers
10k views

Looping through a collection

In the situation where I am looping through a collection looking for an item, is it better practice to break before I return or ...
12
votes
6answers
2k views

Check for balanced parentheses

I was given an assignment to check for balanced parentheses from an arbitrary string where parentheses are defined as (, [ or { and their respective "closing" ...
12
votes
4answers
2k views

Least significant digit (LSD) radix sort of strings

I am learning string algorithms and wrote this C example based on a Java example I found online. Any feedback relating to style, code clarity, whether it is generic enough to be re-used, interface, ...
12
votes
4answers
549 views

Generating an ordinal number for the day of the month

I am relatively new to programming and came across an if statement that I believe to be a bit verbose. I have tested my code and it appears to work without any ...
12
votes
3answers
2k views

Generate all possible combinations of letters in a word

This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome! ...
12
votes
6answers
38k views

Fast way of searching for a string in a text file

I have written this code to search for a string in a .txt file. Is it possible to optimize the code so that it searches for the string in fastest manner possible? ...
12
votes
5answers
343 views

Fastest way of removing a substring from a string

Taking a break from my C++ series, I recently reviewed the code in this question. I ended up writing my own separate derivation of the function so that I could remove all of the substrings from a ...
12
votes
2answers
637 views

Efficiency/design of trim() library code

I am working on some library code and I am trying to optimize my trim() functions. To that effect I am trying to figure out how best to deal with each kind of ...
12
votes
3answers
527 views

Memory-efficient string collection

My goal is to create a memory-efficient (immutable) collection of strings. The imagined use-case is checking for valid words in a Scrabble-like game. Wikimedia Commons has a pretty good summary of ...
12
votes
5answers
19k views

Good method to check whether a string is a valid number?

I'm developing a Java Helper Library which has useful methods developers don't want to type out over and over. I'm trying to think of the best way of checking whether a string is a valid number. I've ...
12
votes
2answers
389 views

Split string in half and change case accordingly

I found the following challenge online: Create a function that takes a string and returns that string with the first half lowercased and the last half uppercased. eg: ...
12
votes
2answers
515 views

Reverse a sentence quickly without pointers

I am trying to reverse a sentence contained in a string and return a string in the quickest way possible using the least amount of memory. Also I don't want to use any unsafe code, so no pointers are ...
12
votes
2answers
849 views

StringBuilder in VBA

In the process of trying to build a serializable data structure, I found myself building large strings, which gets very slow because VBA copies a string every time concatenation is performed. To ...
12
votes
3answers
276 views

C String - new function to detect user's anger - thoughts?

This was the most humerus coding I've ever done. It's for my string library in C. It detects if the user is angry to various degrees, namely str_isHeated(). Why? ...
12
votes
2answers
416 views

Music & DJ IRC bot

I wrote an IRC bot in Java using Pircbot for use in twitch(.tv) chat. It is intended to be run locally and to only be connected to one channel at a time. When it is connected to my channel, it will ...
11
votes
5answers
2k views

Finding longest word in a string

I was working on a problem from Coderbyte. The challenge was to write a function that takes in a string and returns the longest word in the string. If two words are the same size then it asks to ...