Last active
January 8, 2017 10:08
-
-
Save rahman99/996c3965366c8304c533f3395098fa11 to your computer and use it in GitHub Desktop.
String Manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * this is to check letter alphabet: | |
| */ | |
| char chr = 'b'; | |
| chr = (char) (chr + 4); | |
| System.out.println(chr); | |
| --output--> f | |
| note: but this way is not suitable if the next letter is more than z. | |
| /** other way */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * check String ony contain a-z or A-z | |
| * not allow other character | |
| */ | |
| String s1 = "xxxx"; | |
| return !s1.matches("[a-zA-Z]+"); | |
| /** other check way using Class Pattern */ | |
| Pattern p = Pattern.compile("[^a-zA-Z]"); | |
| return p.matcher(s1).find(); | |
| /** if only check a String contain non aphabets, it can use class Character */ | |
| private static boolean checkChar(String s){ | |
| boolean result = false; | |
| for (int i = 0; i < s.length(); i++) { | |
| char charAt2 = s.charAt(i); | |
| result = Character.isLetter(charAt2); | |
| } | |
| return result; | |
| } | |
| /** check length unmatch pattern of a String */ | |
| String s = "valueString"; | |
| return s.replaceAll("[a-m]", "").length(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * count sum of ASCII in String | |
| */ | |
| String s1="SampleofString" | |
| int lengthS1 = 0; | |
| for(char ch1 : s1.toUpperCase().toCharArray()){ | |
| lengthS1 += (int)ch1; | |
| } | |
| return lengthS1; | |
| /** other way using java 8 */ | |
| String s1="valueString"; | |
| return s1.toUpperCase().chars().sum(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static int sorting(int num){ | |
| /** using java 8 */ | |
| return Integer.parseInt(String.valueOf(num) | |
| .chars() | |
| .mapToObj(i -> String.valueOf(Character.getNumericValue(i))) | |
| .sorted(Comparator.reverseOrder()) | |
| .collect(Collectors.joining())); | |
| /** using stringBuilder java 8 */ | |
| if (num < 0) throw new IllegalArgumentException("Negative number: " + num); | |
| return Integer.parseInt(Integer.toString(num).codePoints() | |
| .sorted() | |
| .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | |
| .reverse() | |
| .toString()); | |
| /** using normal way */ | |
| String[] numbers = (Integer.toString(Integer.valueOf(num))).split(""); | |
| Arrays.sort(numbers); | |
| String result = ""; | |
| for(String s : numbers) | |
| { | |
| result = s + result; | |
| } | |
| return Integer.parseInt(result); | |
| /** using String join */ | |
| String[] array = String.valueOf(num).split(""); | |
| Arrays.sort(array, Collections.reverseOrder()); | |
| return Integer.valueOf(String.join("", array)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment