I have two edittexts, et and et2. What I want it to be seached if the string given from et2 is contained in et. I use:
valuez = et.getText().toString();
length = valuez.length();
valuez2 = et2.getText().toString();
length2 = valuez2.length();
for (int i=0; i<length; i++) {
for (int j=0; j<length2; j++) {
if (valuez.charAt(i) != valuez2.charAt(j)) {
continue;
}
if (j == length2) {
System.out.println("Found");
break;
}
}
}
But that doesn't seem to work. Any ideas? Thanks a lot
String valuesz = et.getText().toString();
String valuez2 = et2.getText().toString();
boolean found = (valuez.contains(valuez2));
if (found)
Log.d(TAG, "found");
Related
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
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 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();
}
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);
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 ","