#string-manipulation

15 posts loaded — scroll for more

Link
cloudpunjabi
cloudpunjabi

How to reverse a string in Python? - Cloud Punjabi

Reverse of a string is means simply writing the string from last to the first index in a reverse manner. Likewise, the reverse of “python” will be “nohtyp”.

But, Python does not have any built-in function to reverse a string. However, it can be done in several ways.

photo
Link
cloudpunjabi
cloudpunjabi

Best String Palindrome Program in Python - Cloud Punjabi

A palindrome is a word, number, or any sequence of characters that spell the same whether read in forward or backward direction.

The Python program to check whether the given string is palindrome or not can be created using different methods.

photo
Link
cloudpunjabi
cloudpunjabi

45+ Awesome String Methods in Python | String Functions - Cloud Punjabi

String methods are the built-in function of python used for string manipulation.len() is the basic inbuilt string method. They are also called string functions. However, Python follows the following syntax for using other string functions:.( )

photo
Text
drarkadeep
drarkadeep

An Awkward Analysis of Three Men in a Boat (to say nothing of the dog)

John Green upload this video where one of his fans had taken his book “Looking for Alaska” and had sorted all the words in it alphabetically. The title therefore became “Alaska for Looking”.

I decided to do the same with my favorite book…

View On WordPress

Text
george-pruitt
george-pruitt

Converting A String Date To A Number - String Manipulation in EasyLanguage

Converting A String Date To A Number – String Manipulation in EasyLanguage

EasyLanguage Includes a Powerful String Manipulation Library

I thought I would share this function.  I needed to convert a date string (not a number per se) like “20010115” or “2001/01/15” or “01/15/2001” or “2001-01-15” into a date that TradeStation would understand.  The function had to be flexible enough to accept the four different formats listed above.

String Functions

Most programming…

View On WordPress

Text
jscodetalk-blog
jscodetalk-blog

How To Reverse A String With Built-In Functions

In JavaScript there is no single built-in function to reverse a string so we will use three functions:

1. split(): this will split a String object into a string array
2. reverse(): this will reverse an array
3. join(): this will join all elements of an array into a string.

Here is a complete example:


1
2
3
function reverseString(str) {
    return str.split("").reverse().join("");
}

Text
jscodetalk-blog
jscodetalk-blog

How To Replace All String Occurrences In JavaScript ?

JavaScript’s replace function only removes the first occurrence of the string. To replace of occurrences of a string in another string we can use Regular expressions.

Here is an example of a function that replaces all string occurrences in another string:


1
2
3
function replaceAll(originalString, stringToFind, stringToReplace) {
    return originalString.replace(new RegExp(escapeRegExp(stringToFind), 'g'), stringToReplace);
}

Text
jscodetalk-blog
jscodetalk-blog

How to process each letter of a string

Here is a simple solution to loop trough a string an process each character.


Code:

1
2
3
4
5
6
var str="My string to process."
for (var x = 0; x < str.length; x++)
{
    var c = str.charAt(x);
    alert(c);
}

Photo
copingwithcoding
copingwithcoding

12/100: I studied a good bit and my interview went well, I think! I realized that there was a better way to solve the problem after the interview. So, I went and implemented that and emailed the better solution to my interviewer. I felt good about studying and really knowing my stuff. 

Stationery:

  • BIC brite liner in Green
  • Pilot G-2 0.7 in Teal 
  • Dayal (4G) 0.5mm in Black 

🎧 Trndsttr [M. Maggie, Black Coast] 

photo
Text
the-automators
the-automators

Using the autohotkey StringSplit / StrSplit() function to slide strings

Using the autohotkey StringSplit / StrSplit() function to slide strings

autohotkey StringSplitUsing the AutoHotkey StringSplit / StrSplit() function

AutoHotkey has some pretty cool functions for slicing strings. Not quite as robust as Python however they definitely cover the majority of needs with ease.  In the below video I demonstrate some simple usage if it as well as a way to access the Array it creates without ever saving it to a variable!  :)

Browser_Forward::Reload Browser_Back::…

View On WordPress

Text
bsbonus-blog
bsbonus-blog

Today’s fun with string manipulation, given two strings, test to see if the first can be shifted to match the second. Some example strings:

“atsc”, “cats” ==> yes

“llygaglo”, “lollygag” ==> yes

“cacacaca”, “acacacac”, ==> yes

“elparall”, “elparallx”, ==> no

The trick here is to consider that letters can occur multiple times and to consider how to address the “wrapping” of the word.

My first cut at this, using Ruby, was a pretty ugly combination of small methods, and frankly really way too long. See here if bothered. https://gist.github.com/bsbonus/1958684d52ebe1434233

Obviously, I wasn’t very happy with the attempt. It’s long and does a lot of transformation work that seemed unnecessary. So let’s try recursion!

Here’s the cleaner, recursive version:def reorder_string string1, string2

def reorder_string(string1, string2)
  if string1 == string2
    puts true
  elsif string1.length != string2.length
    puts false
  else
    check_substrings(string1, string2)
  end
end

def check_substrings(string1, string2)
  if string1.length == 0
    puts false
  else
    if string2.include?(string1)
      end_pos = string2.index string1
      puts “#{string2[0…end_pos]}#{string1}”
    else
      string1 = string1[0..-2]
      check_substrings(string1, string2)
    end
  end
end

Technically I could remove the first two if/elseif parts of reorder_string and move them into the check_substrings method, but I think it’s faster to immediately remove the easy cases before entering the recursive steps.

The better question to ask is to determine the tradeoffs between the two. The first attempt finds the starting letter of the second word and finds any locations of it in the first word.

E.g. “ygagloll” and “lollygag” => ‘l’ exists at 4, 6,7 in 'ygagloll’

It then loops over those occurrences, testing at each occurence by transforming the first word at that index so that the word *should* be spelled to match the second word, if the two words match.

Here’s my freshman level algorithmic breakdown.

It’s On to get all the occurrences and then On to check each letter in turn. But in a worst case scenario, all but the last letter of the first word will match the first letter of the second, eg. “aaaaaaaad” and “aaaaaaaac.” Which means ~On occurrences * On checks  = On^2 complexity. 

So in total: On + On^2, so On^2. Gross.

Alternatively, the recursive strategy starts with the largest possible substring match (n-1) in n (e.g. cat in cats). To find a matching string (if it exists) will take On in worst case.

Recursion FTW.

Text
harinilearnsruby
harinilearnsruby

Ruby Shortcuts!

There are obviously several different ways of doing the same thing in Ruby and I love it when I accidentally stumble across cool shortcuts. I was recently solving problems on Rubeque and came across this alternative to using String#split

%w(dracarys is valyrian for dragonfire)  # => ["dracarys", "is", "valyrian", "for", "dragonfire"]

%w(valar\ morghulis valar\ dohaeris) # => ["valar morghulis", "valar dohaeris"]

%w takes a string and splits it on whitespace. With the added bonus of being able to escape whitespace too. It also allows string interpolation. So awesome! 

Link
stevenzeiler
stevenzeiler

Remove Adjacent Duplicate Characters by Character Algorithm

I found myself needing the ability to remove adjacent characters if they matched, but only one character at a time, so here’s my solution.

Link
calebmcelrath
calebmcelrath

Extension Me Strings: ToString Enhancements with Regular Expressions

Embrace the combined power of Regular Expressions with the ease of extension methods in .NET

Text
runandhack-blog
runandhack-blog

Ruby: Reverse a string by word

Question:

Reverse a string - word by word.
For example a string like “This is good day” would be written as
“day good is This”

I tried it in ruby … and it turns out it is just a one liner code …