I got stuck in this regular expression as I am having a string as:
String str = "/abcde/samplename.xyz"
I want to replace this samplename.xyz from a new string, so how can I apply the regular expression in this ?
Try this out.....
String str = "/abcde/samplename.xyz";
String req=str.substring(str.lastIndexOf("/") + 1);
now you get the value of: req=samplename.xyz and you can replace with which ever string value you want
String rep=str.replace(req, "");
String startString=str.substring(0, str.lastIndexOf('/') + 1)
String str = "/abcde/samplename.xyz";
String str1 = str.substring(0, str.lastIndexOf('/') + 1);
Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
Log.i("strArr[1]=", "" + str1);
check this.i hope its useful to you.
Try this::
String str = "/abcde/samplename.xyz";
String result=str.substring(0, str.lastIndexOf('/') + 1);
Log.d("Hello","Result="+result);
String[] result = str.split("/");
String LastItem = result[str.length -1];
LastItem.replace(newString);
Related
I want to get a text that it is a part of an string.For example: I have a string like "I am Vahid" and I want to get everything that it's after "am".
The result will be "Vahid"
How can I do it?
Try:
Example1
String[] separated = CurrentString.split("am");
separated[0]; // I am
separated[1]; // Vahid
Example2
StringTokenizer tokens = new StringTokenizer(CurrentString, "am");
String first = tokens.nextToken();// I am
String second = tokens.nextToken(); //Vahid
Try:
String text = "I am Vahid";
String after = "am";
int index = text.indexOf(after);
String result = "";
if(index != -1){
result = text.substring(index + after.length());
}
System.out.print(result);
Just use like this, call the method with your string.
public String trimString(String stringyouwanttoTrim)
{
if(stringyouwanttoTrim.contains("I am")
{
return stringyouwanttoTrim.split("I am")[1].trim();
}
else
{
return stringyouwanttoTrim;
}
}
If you prefer to split your sentence by blank space you could do like this :
String [] myStringElements = "I am Vahid".split(" ");
System.out.println("your name is " + myStringElements[myStringElements.length - 1]);
So if you are adding in a string, you can just add them via += method(the one i know and using atm). but how can you delete a word in a string/string array?
example: i have a string
String="Monday,Tuesday,Wednesday"
how do you make it into
String="Monday,Wednesday"
any help please?
You could use the replace method.
String sentence = "Monday,Tuesday,Wednesday";
String replaced = sentence.replace("Tuesday,", "");
its easy
just use
yourString = yourString.replaceAll("the text to replace", ""); //the second "" show empty string so the text will get replace by empty string
finally yourString will contain the text u desire Thats it :)
You can use the "public String replace(char oldChar, char newChar)" method if you want to remove "Tuesday" and not the second element
https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string
I think, i will use it for simplicity otherwise go to other suggested answer...
Use Arraylist for storing days:
ArrayList<String> days = new ArrayList<String>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
Use it for creating days string:
public String getDays() {
String daysString = "";
for (int i = 0; i < days.size(); i++) {
if (i != 0)
daysString += ", ";
daysString += days.get(i);
}
return daysString;
}
And whenever you want to remove use
days.remove(1);
or
days.remove("Tuesday");
then again call getDays();
IInd Method if you want to use only string:
String list = "Monday,Tuesday,Wednesday";
System.out.println("New String : " + removeAtIndex(list, 1));
and
public String removeAtIndex(String string, int index) {
int currentPointer = 0;
int lastPointer = string.indexOf(",");
while (index != 0) {
currentPointer = string.indexOf(',', currentPointer) + 1;
lastPointer = string.indexOf(',', lastPointer + 1);
index--;
}
String subString = string.substring(currentPointer,
lastPointer == -1 ? string.length() : lastPointer);
return string.replace((currentPointer != 0 ? "," : "") + subString
+ (currentPointer == 0 ? "," : ""), "");
}
Something like this using a regular expression:
String contents = "Monday,Tuesday,Wednesday";
contents = contents.replaceAll("[\\,]+Tuesday|^Tuesday[\\,]*", "");
I have a String separated by commas as follows
1,2,4,6,8,11,14,15,16,17,18
This string is generated upon user input. Suppose the user wants to remove any of the numbers, I have to rebuild the string without the specified number.
If the current string is:
1,2,4,6,8,11,14,15,16,17,18
User intents to remove 1, the final string has to be:
2,4,6,8,11,14,15,16,17,18
I tried to achieve this using the following code:
//String num will be the number to be removed
old = tv.getText().toString(); //old string
newString = old.replace(num+",",""); //will be the new string
This might be working sometimes but it is sure that it won't work for the above example I have shown, if I try to remove the 1, it also removes the last part of 11, because there also exists 1.
well you can use this. Its the most simplest approach i can think of:
//String num will be the number to be removed
old=","+tv.getText().toString()+",";//old string commas added to remove trailing entries
newString=old.replace(","+num+",",",");// will be the new string
newString=newString.substring(1,newString.length()-1); // removing the extra commas added
This would work for what you want to do. I have added a comma at the start and end of your string so that you can also remove the first and last entries too.
You can split the string first and check for the number where you append those value that is not equivalent to the number that will get deleted;
sample:
String formated = "1,2,4,6,8,11,14,15,16,17,18";
String []s = formated.split(",");
StringBuilder newS = new StringBuilder();
for(String s2 : s)
{
if(!s2.equals("1"))
newS.append(s2 + ",");
}
if(newS.length() >= 1)
newS.deleteCharAt(newS.length() - 1);
System.out.println(newS);
result:
2,4,6,8,11,14,15,16,17,18
static public String removeItemFromCommaDelimitedString(String str, String item)
{
StringBuilder builder = new StringBuilder();
int count = 0;
String [] splits = str.split(",");
for (String s : splits)
{
if (item.equals(s) == false)
{
if (count != 0)
{
builder.append(',');
}
builder.append(s);
count++;
}
}
return builder.toString();
}
String old = "1,2,4,6,8,11,14,15,16,17,18";
int num = 11;
String toRemove = "," + num + "," ;
String oldString = "," + old + ",";
int index = oldString.indexOf(toRemove);
System.out.println(index);
String newString = null;
if(index > old.length() - toRemove.length() + 1){
newString = old.substring(0, index - 1);
}else{
newString = old.substring(0, index) + old.substring(index + toRemove.length() -1 , old.length());
}
System.out.println(newString);
Example: I have a EditText and I want to check the first word is the city name and second word is the pincode. These both words are separated by comma(,).
Hey try this if you don't want to use split. YOu need to get string into a variable from edittext and then use the following code for doing yourself able to validate :)
String str = "tim,52250";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Do this way..
String content="Mehsana,384001";
String[] contentArray=content.split(",");
And you will get
contentArray[0]=Mehsana
contentArray[1]=384001
then you can validate each string content..
Use split() to get the things done.
Ex:
String s= "abc,123"
String s1[]=s.split(",");
String city=s1[0];
String pincode=s1[1];
Try this
String strInput = editText.getText().toString();
String strSplit [] = strInput.split(",");
System.out.println("CityName : " + strSplit[0]);
System.out.println("PinCode : " + strSplit[1]);
String data = "ali,524513"
String []array = data.split(",")
you can validate array[0] and array[1] :)
System.out.println("Name: "+array[0]+" code: "+array[1]);
This doesn't work:
int number = 1;
String numberstring = IntToString(number);
I get "The method IntToString(int) is undefined for the type (myclassname)"
I must be tired here. What the heck am I forgetting?
Try this.
int number = 1;
String numberstring = Integer.toString(number);
You can do either:
String numberstring = number.toString();
or
String numberstring = Integer.toString(number)
or (as a tricky thing sometimes I do this)
String numberstring = 1 + "";
Why not this :
int number = 1;
String numberstring = number+"";
Also make sure that :
Is there IntToString method in your class file - myclassname? Is the arguement types are matching?
These ones are the ones that seems to work "out of the box", without importing any special classes:
String numberstring = String.valueOf(number);
String numberstring = Integer.toString(number);
String numberstring = number + "";
One way to do this with very little code would be like this:
int number = 1;
String numberstring = number + "";
Are you expecting this -
int numb = 1;
String val = String.valueOf(numb);
int number=1;
String S = String.valueOf(number);
try this code... works for me :)
In Java, int to String is simple as
String numberstring = String.valueOf(number);
This applies to Android too.