Substring

Revision as of 05:50, 5 August 2009 by rosettacode>Oligomous (Created page with '{{Task|Basic language learning}}Category:String manipulations {{basic data operation}} In this task display a substring: * starting from <tt>n</tt> characters in and of <tt>…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this task display a substring:

  • starting from n characters in and of m length;
  • starting from n characters in, up to the end of the string;
  • whole string minus last character;
  • starting from a known character within the string and of m length;
  • starting from a known substring within the string and of m length.
Task
Substring
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

PHP

<lang php><?php $str = 'abcdefgh'; $n = 2; $m = 3; echo substr($str, $n, $m); echo substr($str, $n); echo substr($str, 0, -1); echo substr($str, strpos($str, 'd'), $m); echo substr($str, strpos($str, 'de'), $m); </lang>

Ruby

<lang ruby> str = 'abcdefgh' n = 2 m = 3 puts str[n, m] puts str[n..-1] puts str[0..-2] puts str[str.index('d'), m] puts str[str.index('de'), m] </lang>