Check if next char in a string is a space - android

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);

Related

Android. How to switch String = (A B, C D, E F, .....) to String (B A, D C, F E, ......)

i am a beginner.
I get String from SQLite.
WKT = cursor.getString(cursor.getColumnIndex(polygon));
WKT = WKT.replaceAll(",", ", ");
Its look like String = (1.00 2.00, 3.00 4.00, 5.00 6.00, .....).
How to switch it to String (2.00 1.00, 4.00 3.00, 6.00 5.00, ......).
Thanks.
try this
private static void testMethod() {
String result = "(A B, C D, E F)";
String resultTmp = result;
resultTmp = resultTmp.replace("(", "");
resultTmp = resultTmp.replace(")", "");
String[] aryResult = resultTmp.split(",");
String[] finalResult = reverseString(aryResult);
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < finalResult.length; i++) {
String dfgf = strBuilder.toString().equals("") ? "" : ",";
strBuilder.append(dfgf);
strBuilder.append(finalResult[i]);
}
String newString = strBuilder.toString();
System.out.println("RESULT : " + newString);
}
public static String[] reverseString(String[] words)
{
String[] t = new String[words.length];
for (int i = 0; i < words.length; i++)
{
t[i] = "";
for (int j = words[i].length() - 1; j >= 0; j--)
t[i] += words[i].charAt(j);
}
return t;
}
Follow below steps
1)Split string with ","
2)User Reverse() function to reverse the string(AB->BA)
3)Store the result in to an array.
Repeat the step until you split the last entry from sql lite.
Try this logic,its working in c and c++.
int Groups = 1; // Count 1 for the first group of letters
for ( int Loop1 = 0; Loop1 < strlen(String); Loop1++)
if (String[Loop1] == ' ') // Any extra groups are delimited by space
Groups += 1;
int* GroupPositions = new int[Groups]; // Stores the positions
for ( int Loop2 = 0, Position = 0; Loop2 < strlen(String); Loop2++)
{
if (String[Loop2] != ' ' && (String[Loop2-1] == ' ' || Loop2-1 < 0))
{
GroupPositions[Position] = Loop2; // Store position of the first letter
Position += 1; // Increment the next position of interest
}
}
try this..
private static void testTwo() {
String result = "(1.00 2.00, 3.00 4.00, 5.00 6.00)";
String resultTmp = result;
resultTmp = resultTmp.replace("(", "");
resultTmp = resultTmp.replace(")", "");
String[] aryResult = resultTmp.split(",");
for (int i = 0; i < aryResult.length; i++) {
String str = aryResult[i].trim();
String[] sdf = str.split(" ");
aryResult[i] = reverseArray(sdf);
}
String strResult = Arrays.toString(aryResult).replace("[", "(").replace("]", ")");
System.out.println("RESULT : " + strResult);
}
public static String reverseArray(String[] words)
{
for(int i = 0; i < words.length / 2; i++)
{
String temp = words[i];
words[i] = words[words.length - i - 1];
words[words.length - i - 1] = temp;
}
return ((Arrays.toString(words)).replace("[", "")).replace("]", "").replace(",", "");
}
#. If you want to swap sub-string, just use below method:
public String swapString(String str) {
str = str.replace("(", "");
str = str.replace(")", "");
String[] array = str.split(", ");
StringBuilder result = new StringBuilder();
result.append("(");
for (int i = 0; i < array.length; i++) {
String[] temp = String.valueOf(array[i]).split(" ");
result.append(temp[1]);
result.append(" ");
result.append(temp[0]);
if (i != array.length -1)
result.append(", ");
}
result.append(")");
return result.toString();
}
USE:
String yourString = "(1.00 2.00, 3.00 4.00, 5.00 6.00)";
Log.d("ARRAY", "Before Swap: " + yourString);
yourString = swapString(yourString);
Log.d("ARRAY", "After Swap: " + yourString);
OUTPUT:
D/ARRAY: Before Swap: (1.00 2.00, 3.00 4.00, 5.00 6.00)
D/ARRAY: After Swap: (2.00 1.00, 4.00 3.00, 6.00 5.00)
#. If you want to swap char, use below method:
public String swapChar(String str) {
// Convert string to char array
char[] array = str.toCharArray();
for (int i = 1; i < array.length - 1; i+=5) {
// Swap char
char temp = array[i];
array[i] = array[i+2];
array[i+2] = temp;
}
return String.valueOf(array);
}
USE:
String yourString = "(A B, C D, E F, G H, I J)";
Log.d("ARRAY", "Before Swap: " + yourString);
yourString = swapChar(yourString);
Log.d("ARRAY", "After Swap: " + yourString);
OUTPUT:
D/ARRAY: Before Swap: (A B, C D, E F, G H, I J)
D/ARRAY: After Swap: (B A, D C, F E, H G, J I)
Hope this will help~

for loop overextending index

I'm attempting to iterate through the EditText "name" and check the letters so see if they match any of the letters in the objects of the array uNamesList.
If they do I want to break out of the loop and return the placement of the object that had the matching letter. At the moment I am getting this error, anyone know what might be wrong?
java.lang.StringIndexOutOfBoundsException: length=9; index=9
uNamesList.add("bob");
uNamesList.add("mike");
uNamesList.add("sike");
uNamesList.add("othername");
uNamesList.add("name");
public int getName(EditText name) {
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
break;
}
}
}
return match;
}
You get the text inside edit text in wrong way. it should :
public int getName(EditText name) {
String text = name.getText(); //get the text
int match = 10;
for (int i = 0; i < text.toString().length(); i++) { //get the length of the text
for (int o = 0; o < uNamesList.size(); o++) {
char cLetter = name.toString().charAt(i);
String uName = (String)uNamesList.get(o);
Okay, here's the edit for another problem. i dont know if this what you want.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
boolean found = false;
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
found = true;
break;
}
}
if(found) break;
}
return match;
}
The simplest way is just only call return when it found.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
return o;
}
}
}
return match;
}
change your for loop to the following, you accessing name from the uNameList loop where the length might be different
for (int i = 0; i < name.toString().length(); i++) {
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
For EditText you need GetText()
name.toString() gives you the java desciption of the object EditText
You are getting a StringIndexOutOfBoundsException most likely due to this line:
char uLetter = uName.charAt(i);
i in this case can be greater than the length of uName

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)

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

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();
}

regular-expressions android

i have string like these for example
309\306\308\337_338
309\306\337_338
310
311\315_316\336_337
311\315_316\336_337
311\335_336
these strings means list of page number , for example string "309\306\308\337_339" means
pages 309,306,308,337,338,339
i want to pass one of these string to function which return it as string like this
309,306,308,337,338,339
this function do that but in c# , i want to impalement in android
private static string Get_PageNumbers(string str)
{
ArrayList arrAll = new ArrayList();
MatchCollection match;
string[] excar;
string strid, firstNumber, lastlNumber;
int fn, ln;
ArrayList arrID = new ArrayList();
//***In Case The Range Number Between "_"
if (str.Contains("_"))
{
// match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
match = matchReg.Matches(str);
int count = match.Count;
excar = new string[0];
for (int i = 0; i < count; i++)
{
Array.Resize(ref excar, count);
excar[i] = match[i].Groups[0].Value;
if (excar[i] != string.Empty)
arrID.Add(excar[i]);
}
//******IF Array Contains Range Of Number Like"102_110"
if (str.Contains("_"))
{
for (int i = 0; i < arrID.Count; i++)
{
strid = arrID[i].ToString();
if (arrID[i].ToString().Contains("_"))
{
int idy = strid.LastIndexOf("_");
firstNumber = strid.Substring(0, idy);
if (idy != -1)
{
lastlNumber = strid.Substring(idy + 1);
fn = int.Parse(firstNumber);
arrAll.Add(fn);
ln = int.Parse(lastlNumber);
for (int c = fn; c < ln; c++)
{
fn++;
arrAll.Add(fn);
}
}
}
else
{
arrAll.Add(arrID[i].ToString());
}
}
//******If Array Contain More Than One Number
if (arrAll.Count > 0)
{
str = "";
for (int i = 0; i < arrAll.Count; i++)
{
if (str != string.Empty)
str = str + "," + arrAll[i];
else
str = arrAll[i].ToString();
}
}
}
}
//***If string Contains between "/" only without "_"
else if (str.Contains("/") && !str.Contains("_"))
{
str = str.Replace("/", ",");
}
else if (str.Contains("\\"))
{
str = str.Replace("\\", ",");
}
return str;
}
I think this is easier to do with split function:
public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
String result = "";
String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
for (int i = 0; i < pages.length; i++) {
String page = pages[i];
int index = page.indexOf('_');
if (index != -1) { // special case i.e. "337_338", index = 3
int start = Integer.parseInt(page.substring(0, index)); // start = 337
int end = Integer.parseInt(page.substring(index + 1)); // end = 338
for (int j = start; j <= end; j++) {
result += String.valueOf(j);
if (j != end) { // don't add ',' after last one
result += ",";
}
}
} else { // regular case i.e. "309","306","308"
result += page;
}
if (i != (pages.length-1)) { // don't add ',' after last one
result += ",";
}
}
return result; // result = "309,306,308,337,338"
}
For example this function when called as follows:
String result1 = Get_PageNumbers("309\\306\\308\\337_338");
String result2 = Get_PageNumbers("311\\315_316\\336_337");
String result3 = Get_PageNumbers("310");
Returns:
309,306,308,337,338
311,315,316,336,337
310
if i can suggest different implementation....
first, split string with "\" str.split("\\");, here you receive an array string with single number or a pattern like "num_num"
for all string founded, if string NOT contains "" char, put string in another array (othArr named), than, you split again with "" str.split("_");, now you have a 2 position array
convert that 2 strings in integer
now create a loot to min val form max val or two strings converted (and put it into othArr)
tranform othArr in a string separated with ","

Categories

Resources