Click or drag to resize

CharMix Function

Return a string whose odd-numbered characters and even-numbered characters are from 2 different strings.

Namespace:  XSharp.Core
Assembly:  XSharp.Core (in XSharp.Core.dll) Version: 2.2a
Syntax
 FUNCTION CharMix(
	cOdd AS STRING,
	cEven AS STRING
) AS STRING
View Source

Parameters

cOdd
Type: String
The string whose characters will constitute the odd-numbered characters in the returned string.
cEven
Type: String
The string whose characters will constitute the even-numbered characters in the returned string.

Return Value

Type: String
A string in which characters from <cOdd> and <cEven> appear alternately.  The length of <cOdd> determines the length of the returned string: the length is twice the length of <cOdd>, assuming that <cEven> contains at least one character: A longer <cEven> string is cut down to the length of <cOdd>. A shorter <cEven> is processed from beginning to end, wrapping to the beginning again until there are no more characters in <cOdd>. If <cEven> contains less than one character, a NULL_STRING is returned.
Remarks
CharMix() provides a convenient way to recombine strings extracted from CharOdd() and CharEven().
Examples
This example mixes 2 strings:
X#
? CharMix("13579", "24680")        // 1234567890
This example  shuffles the cards in a game:
X#
Function Start()
        LOCAL cPoker AS STRING
        cPoker := "AKJQ0123456789"
        ? Shuffle(cPoker, 10)        // Q37A048K159J26
        RETURN TRUE
FUNCTION Shuffle(cDeck AS STRING, ;
                siShuffles AS SHORTINT) AS STRING PASCAL
        // Assumes even number of cards.
        // Length of cDeck is divisible by 2
        LOCAL siCounter AS SHORTINT
        LOCAL siCards AS SHORTINT
        siCards := SLen(cDeck)
        // First and last cards are not shuffled;
        // so add/pad blank cards
        siCards += 2
        cDeck := PadC(cDeck, siCards)
        FOR siCounter := 1 UPTO siShuffles
                // Cut the deck in half and shuffle
                cDeck := CharMix(Substr3(cDeck, 1,;
                siCards/2), Substr2(cDeck, (siCards/2)+1))
        NEXT
        RETURN Substr3(cDeck, 2, siCards-2)
        // Exclude added blank cards
See Also