/****************************************************************************** * This program demonstrates basic string functions. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class Strings2 { public static void main(String[] args) { final String alphabet = "abcdefghijklmnopqrstuvwxyzabc"; final String greekAlphabet = "αβγδεζηθικλμνξοπρσςτυφχψωαβγ"; final String emoji = "😃😇🥰🤪🤑😴🤒🥵🥶🤯🥳😎😥😱😡🤬💀👽🤖😺🙈🙉🙊😃😇🥰"; System.out.println("Length: " + Utils.cpLength(alphabet)); System.out.println("charAt(17): " + Utils.uniAt(alphabet, 17)); System.out.println("codePointAt(17): " + Utils.cpAt(alphabet, 17)); System.out.println("substr(23, 26): " + Utils.cpSubstring(alphabet, 23, 26)); System.out.println("prefix(6): " + Utils.cpSubstring(alphabet, 0, 6)); System.out.println("right_tail(6): " + Utils.cpSubstring(alphabet, 6)); System.out.println("suffix(6): " + Utils.cpSubstring(alphabet, Utils.cpLength(alphabet) - 6)); System.out.println("find(\'def\'): " + Utils.cpIndexOf(alphabet, "def")); System.out.println("find(\'def\') is not found: " + (Utils.cpIndexOf(alphabet, "def") == -1)); System.out.println("find(\'bug\'): " + Utils.cpIndexOf(alphabet, "bug")); System.out.println("find(\'bug\') is not found: " + (Utils.cpIndexOf(alphabet, "bug") == -1)); System.out.println("rfind(\'abc\'): " + Utils.cpLastIndexOf(alphabet, "abc")); System.out.println("rfind(\'abc\') is not found: " + (Utils.cpLastIndexOf(alphabet, "abc") == -1)); System.out.println("rfind(\'bug\'): " + Utils.cpLastIndexOf(alphabet, "bug")); System.out.println("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(alphabet, "bug") == -1)); System.out.println("Length: " + Utils.cpLength(greekAlphabet)); System.out.println("charAt(17): " + Utils.uniAt(greekAlphabet, 17)); System.out.println("codePointAt(17): " + Utils.cpAt(greekAlphabet, 17)); System.out.println("substr(23, 26): " + Utils.cpSubstring(greekAlphabet, 23, 26)); System.out.println("prefix(6): " + Utils.cpSubstring(greekAlphabet, 0, 6)); System.out.println("right_tail(6): " + Utils.cpSubstring(greekAlphabet, 6)); System.out.println("suffix(6): " + Utils.cpSubstring(greekAlphabet, Utils.cpLength(greekAlphabet) - 6)); System.out.println("find(\'δεζ\'): " + Utils.cpIndexOf(greekAlphabet, "δεζ")); System.out.println("find(\'δεζ\') is not found: " + (Utils.cpIndexOf(greekAlphabet, "δεζ") == -1)); System.out.println("find(\'bug\'): " + Utils.cpIndexOf(greekAlphabet, "bug")); System.out.println("find(\'bug\') is not found: " + (Utils.cpIndexOf(greekAlphabet, "bug") == -1)); System.out.println("rfind(\'αβγ\'): " + Utils.cpLastIndexOf(greekAlphabet, "αβγ")); System.out.println("rfind(\'αβγ\') is not found: " + (Utils.cpLastIndexOf(greekAlphabet, "αβγ") == -1)); System.out.println("rfind(\'bug\'): " + Utils.cpLastIndexOf(greekAlphabet, "bug")); System.out.println("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(greekAlphabet, "bug") == -1)); /****************************************************************************** * Because strings are represented using UTF-16 it takes two characters to * represent one codepoint for these emoji. So lengths and positions are twice * what we normally would expect when dealing with Unicode characters. So the * standard string functions basically only work correctly with chraracters from * the BMP. Use the functions from Utils module to work correctly with all * Unicode characters. *****************************************************************************/ System.out.println("Length: " + Utils.cpLength(emoji)); System.out.println("charAt(16): " + Utils.uniAt(emoji, 16)); System.out.println("codePointAt(16): " + Utils.cpAt(emoji, 16)); System.out.println("substr(20, 24): " + Utils.cpSubstring(emoji, 20, 24)); System.out.println("prefix(6): " + Utils.cpSubstring(emoji, 0, 6)); System.out.println("right_tail(6): " + Utils.cpSubstring(emoji, 6)); System.out.println("suffix(6): " + Utils.cpSubstring(emoji, Utils.cpLength(emoji) - 6)); System.out.println("find(\'😱😡🤬\'): " + Utils.cpIndexOf(emoji, "😱😡🤬")); System.out.println("find(\'😱😡🤬\') is not found: " + (Utils.cpIndexOf(emoji, "😱😡🤬") == -1)); System.out.println("find(\'bug\'): " + Utils.cpIndexOf(emoji, "bug")); System.out.println("find(\'bug\') is not found: " + (Utils.cpIndexOf(emoji, "bug") == -1)); System.out.println("rfind(\'😃😇🥰\'): " + Utils.cpLastIndexOf(emoji, "😃😇🥰")); System.out.println("rfind(\'😃😇🥰\') is not found: " + (Utils.cpLastIndexOf(emoji, "😃😇🥰") == -1)); System.out.println("rfind(\'bug\'): " + Utils.cpLastIndexOf(emoji, "bug")); System.out.println("rfind(\'bug\') is not found: " + (Utils.cpLastIndexOf(emoji, "bug") == -1)); } }