How add "-" before each capital letter in a string - android

How can i add "-" before each capital letter of my string apart from first capital letter of my string.
I have a string like this "HelloWorldMyNameIsCarl" and i am using this
"HelloWorldMyNameIsCarl".replaceAll("(.)(\\p{Lu})", "$1-$2")
it's working fine.
solution is
"Hello_World_My_Name_Is_Carl"
but for "THisForNEWTest" it's not working and solution is
"T-His-For-NEw-Test"
But i want
"T-His-For-N-Ew-Test"
please suggest me what i do for this problem.
thanks.

if there is a too complex problem for regular expressions, you can always use normal programming. it might even be a little bit more efficient :
public static String doIt(String input)
{
int size=input.length();
if(size==0)
return "";
StringBuilder sb=new StringBuilder(size);
sb.append(input.charAt(0));
for(int i=1;i<size;++i)
{
char c=input.charAt(i);
if(Character.isUpperCase(c))
sb.append('-');
sb.append(c);
}
return sb.toString();
}
in any case, for regular expressions tests, you can check out this website.
so, for regular expressions, the solution can be:
return input.charAt(0)+input.substring(1).replaceAll("(\\p{Lu})","-$1");

Why aren't you just doing this:
replaceAll("(\\p{Lu})", "-$1").replaceAll("^-", "")

Try Below Code:
String test = "THisForNEwTest";
int size = test.length();
StringBuffer sb = new StringBuffer();
if(size!=0)
sb.append(test.charAt(0));
for (int i = 1; i < size; i++) {
if(Character.isUpperCase(test.charAt(i))){
sb.append("-"+test.charAt(i));
}else{
sb.append(test.charAt(i));
}
}
System.out.println("result is::::"+sb.toString());

Try this:
public static String function(String str) {
StringBuilder result = new StringBuilder();
List<Integer> capital = new ArrayList<Integer>();
for (int i = 0; i < str.length(); i++)
if (Character.isUpperCase(str.charAt(i)))
capital.add(i);
int capIndex = 0;
int x = 0;
for (int y = 0; y < str.length(); y++) {
if(x < capital.size())
capIndex = capital.get(x);
if (y == 0) {
result.append(str.charAt(y));
x++;
} else if(y == capIndex){
result.append("-" + str.charAt(y));
x++;
} else {
if(str.charAt(y) != ' ')
result.append(str.charAt(y));
}
}
return result.toString();
}

Related

How do I create a 2 dimensional array from a text file in Android?

How can make a two dimensional Array of integers by reading from a .txt file that looks like this:
0000
0100
1233
Would you use BufferedReader or InputStream?
Here is what I have so far and it either crashes, or it just says 52, 52, 52....
public static void loadTileMap(String fileName, int height, int width) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(GameMainActivity.assets.open(fileName)));
String line;
tileArray = new int[width][height];
while (true) {
line = reader.readLine();
if (line == null) {
reader.close();
break;
}
for (int i = 0; i < width; i++) {
String string = line.toString();
for (int j = 0; j < height; j++) {
if (j < string.length()) {
int k = (int)string.charAt(j);
tileArray[i][j] = k;
}
}
}
}
}
Bufferdreader. Also to make a 2D array like that the steps should look like this:
1 - make the 2D array
2 - as you read watch line add it to the array[line#][0]
Also, you are converting the char into an int. Thereby causing it to change to its Unicode (or ASCII I don't know) representation e.g. 52.
OK thank you for all your help! I did what CyberGeek.exe suggested but I modified it a bit. Here is my code:
public static int[][] tileArray;
public static void loadTileMap(String fileName, int height, int width) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(GameMainActivity.assets.open(fileName)));
String line;
tileArray = new int[width][height];
for (int i = 0; i < width; i++) {
line = reader.readLine();
if (line == null) {
reader.close();
}
for (int j = 0; j < height; j++) {
int k = Integer.parseInt(line.substring(j, j+1));
tileArray[i][j] = k;
}
}
}
There might be an easier way but I'm not sure. Either way this works for me!

Android hide numbers like password

I want to know how to hide some numbers in TextView and some are shown just like that
(****-****-1234)
Thank you
Try This Method...
public static String StrRpl(String str) {
char[] chars = str.toCharArray();
for (int i = 0, j = 0; i < chars.length && j < 5; i++) {
char ch = chars[i];
if (!Character.isWhitespace(ch)) {
chars[i] = '*';
j++;
}
}
str = new String(chars);
return str;
}
Output : *****234
pass a string to this method and it will return string with '*' character till first 5 characters (You can change your number of count.. current is 5)

decimal to ascii android

I am attempting to implement a conversion from a decimal number to ascii. Right now, i've hard coded it to 50 but I will use userinput later on. When I run the application and call the method, what is outputed is random (it changes everytime I press it) eg "[C#4263f600"
I followed the process in the link: decimal to binary. Why am I getting such a weird output?
//method
if (valid) {
String str = ascii();
mTextOutput.setText(str);
...
public String ascii(){
char[] binary_reverse = new char[9];
char[] binary = new char[9];
int ascii = 50;
int y = 0;
while (ascii !=1) {
if (ascii % 2 ==0)
{
binary_reverse[y]='0';
}
else if (ascii % 2 == 1)
{
binary_reverse[y]='1';
}
ascii /= 2;
y++;
}
if (ascii ==1)
{
binary_reverse[y] = '1';
}
if (y<8) {
for(; y < 8; y++) {
binary_reverse[y] = '0';
}
}
for (int z = 0; z < 8; z++) {
binary[z] = binary_reverse[7-1];
}
String str = binary.toString();
return str;
}
//You can try this :
binary[z] = binary_reverse[7-1]; - So you want to set every element of binary to the value in binary_reverse[6]
//and also
String str = binary.toString(); to String str = new String(binary);
reference : #Tim

Arabic digit to English digit android

I am using this code for translate English digit into Arabic. But now i am trying to change Arabic digits into English digit.
private void decimalToArabic() {
String str = showOutputEdit.getText().toString();
char[] arabicChars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
// char[] arabicChars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
StringBuilder builder = new StringBuilder();
for(int i =0;i<str.length();i++)
{
if(Character.isDigit(str.charAt(i)))
{
builder.append(arabicChars[(int)(str.charAt(i))-48]);
}
else
{
builder.append(str.charAt(i));
}
}
System.out.println("Number in English : "+str);
System.out.println("Number In Arabic : "+builder.toString() );
showOutputEdit.setText(builder.reverse().toString());
}
what should i need to change?
i think you can use something like this.(Here i convert integer to arabic)
public String convertToArabic(int value)
{
String newValue = (((((((((((value+"").replaceAll("1", "١")).replaceAll("2", "٢")).replaceAll("3", "٣")).replaceAll("4", "٤")).replaceAll("5", "٥")).replaceAll("6", "٦")).replaceAll("7", "٧")).replaceAll("8", "٨")).replaceAll("9", "٩")).replaceAll("0", "٠"));
return newValue;
}
private static String arabicToenglish(String number)
{
char[] chars = new char[number.length()];
for(int i=0;i<number.length();i++) {
char ch = number.charAt(i);
if (ch >= 0x0660 && ch <= 0x0669)
ch -= 0x0660 - '0';
else if (ch >= 0x06f0 && ch <= 0x06F9)
ch -= 0x06f0 - '0';
chars[i] = ch;
}
return new String(chars);
}
arabic chars array must be reversed
WRONG
char[] chars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
RIGHT
char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
It is just because of stackoverflow editor, but if you will copy text and paste (CTRL + C CTRL + V) it will work just fine.
so function will look like this
private static String toArabicDigits(String eng) {
char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < eng.length(); ++i) {
if (Character.isDigit(eng.charAt(i))) {
builder.append(chars[(int)(eng.charAt(i))-48]);
System.out.println("char - " + eng.charAt(i) + " " + (int)(eng.charAt(i)-48) + " - " + chars[(int)(eng.charAt(i))-48]);
} else {
builder.append(eng.charAt(i));
}
}
return builder.toString();
}
public String convertArabic(String arabicStr) {
char[] chArr = arabicStr.toCharArray();
StringBuilder sb = new StringBuilder();
for (char ch : chArr) {
if (Character.isDigit(ch)) {
sb.append(Character.getNumericValue(ch));
} else {
sb.append(ch);
}
}
return sb.toString();
}

Check if next char in a string is a space

I would like to check if the current char is a blank space as i dont want to change that. So i would like to change all the letters like i do now, but keep the spaces.
e.g. "that man" with C = 2 should become "vjcv ocp".
thanks in advance :)
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
C = Integer.valueOf(ceasarNr);
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);
}
Just skip the iteration if the character is a space:
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
//Skip if it is space.
if chars[i-1] == ' ';
continue;
C = Integer.valueOf(ceasarNr);
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);
}
There is method for checking if char is blank space ->
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
C = Integer.valueOf(ceasarNr);
if(!Character.isWhitespace(chars.charAt(i-1)) {
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);

Categories

Resources