-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStringHelper.java
59 lines (51 loc) · 1.55 KB
/
StringHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* $Header$
*
* Copyright (C) 2019 Cefalo AS.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from Cefalo AS. This notice must be included on all
* copies, modifications and derivatives of this work.
*/
package com.cefalo.tdd;
/**
* @author <a href="mailto:fmshaon@gmail.com">Ferdous Mahmud Shaon</a>
* @author last modified by $Author$
* @version $Revision$ $Date$
*/
public class StringHelper {
/*
* Sample Input and Output
* "BCDE" => "BCDE"
* "ABCD" => "BCD"
* "AACD" => "CD"
* "BACD" => "BCD"
* "AAAA" => "AA"
* "MNAA" => "MNAA"*
* "A" => ""
* "" => ""
*/
public static String truncateFirst2As(final String pInput) {
if(pInput.length()<2) return pInput.replaceAll("A", "");
String firstTwoChars = pInput.substring(0,2);
String remainingChars = pInput.substring(2);
String output = firstTwoChars.replaceAll("A","").concat(remainingChars);
return output;
}
/*
* Sample input and Output:
* "AB" => "BA"
* "ABCD" => "ABDC"
* "AACDEFGHIJ" => "AACDEFGHJI"
* "A => "A"
* "" => ""
*/
public static String swapLastTwoChars(final String pInput) {
final int length = pInput.length();
if(length<2) return pInput;
String inputWithoutLastTwoChars = pInput.substring(0,length-2);
String lastTwoChars = pInput.substring(length-2);
String reverseLasTwoChars = new StringBuilder(lastTwoChars).reverse().toString();
return inputWithoutLastTwoChars.concat(reverseLasTwoChars);
}
}