A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

Bash parameter expansion

Have you ever seen ## (hash hash/pound pound) or %% (percent percent) inside a bash script and wondered what it means? It's a form of parameter expansion that allows you to manipulate your strings using regexes.

All the following commands work with a variable called $string.
'pattern' means bash pattern, or can also be an ordinary string:

  • String length: ${#string}
  • Extract a substring: ${string:position}
  • Extract a substring, specifying length: ${string:position:length}
  • Delete shortest match of substring from the beginning of string: ${string#substring}
  • Delete shortest match of substring from the end of string: ${string%substring}
  • Delete longest match of substring from the beginning of string: ${string##substring}
  • Delete longest match of substring from the end of string: ${string%%substring}
  • Find and replace first substring: ${string/pattern/replacement}
  • Find and replace all substrings: ${string//pattern/replacement}

Thanks to TheGeekStuff

Sprintf in Java

import java.text.MessageFormat;

int a = 3;
String b = "coders";

System.out.println(
    MessageFormat.format("{0} Java {1}.", String.format("%04d", a), b)
);

PHP arrays

This won't work:
php -r "$a = array( 'c' => array( 'b' => 1 ) );"

But this will:
php -r '$a = Array( "c" => Array( "b" => 1 ) );'

On Mac OSX

PHP regular expressions


/(.*)something/s

The s at the end causes the dot to match all characters including newlines.

Use a string to make a variable name

my $rank10 = 42;
{
    no strict 'refs';
    my $num = 10;
    print eval "\$rank$num";
}


result:
    42

Perl sprintf

        # Format number with up to 8 leading zeroes
$result = sprintf("%08d", $number);
        # Round number to 3 digits after decimal point
$rounded = sprintf("%.3f", $number);

Perl's sprintf permits the following universally-known conversions:

   %% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
%g a floating-point number, in %e or %f notation

Regular expressions in C: Advice

If you can avoid using regular expressions in C, then *do not use them!!*

Instead try the strstr (find substring) and strtok (like perl's split) routines.

Comparing strings in C

In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid. Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.

if(strcmp(name1,name2)==0)
 puts("The names are the same");
else
 puts("The names are not the same.");

Thanks johnt