i have a string sssssh and i want to get the result as h only. it only can solve while i have another string ssssth and i will get th also without the s in front. whether i need to split it? can anyone help me?
i don't want to use - to separate by inserting the split coding since my string is like this.
before this i originally the string is h then i using String.format to insert ssss in front of the h. lastly i need to remove all the s and get the original string h.
String str1 = ssssh;
String result = h;
You could use the String method to get the index of the last character 's':
int lastOccurenceOfS = str1.lastIndexOf("s");
Then you split your string from the next character forward:
String endString = str1.subString(lastOccurenceOfS + 1);
If what your asking is to split the h from all the other characters you can use this:
String str1 = "ssssh";
char ch = str1.charAt(str1.length() - 1);
Then what you are left with is the Char ch(h)
Related
I want to keep all the characters before a space and remove the rest of the string.
Example:
String str = "Davenport FL 33897 US";
I want to print only "Davenport" and remove the rest of the string. Also the string is dynamic every time.
You need to use substr and indexOf
str.substr(0,str.indexOf(' ')); // "Davenport"
substr(0,X) will give you the sub string of your string starting at index 0 and ending at index X,
Because you want to get the first word until the first space you replace X with:
str.indexOf(' ')
which returns the index of the first space in your string
We can use regular expression to detect space and special character. You can utilize below code to do the same.
String str="Davenport FL 33897 US";
String[] terms = str.split("[\\s#&.?$+-]+");
System.out.println(terms[0]);
It's working for me.
You can use the split function to split the string using ' ' as your delimeter.
const myString = "Davenport FL 33897 US"
const splitString = myString.split(' ')
split() returns an array of strings that separated using the delimeter.
you can just get the index of the first string by.
console.log(splitString[0])
another way would be just to put the index after the split function so that you get only what you want and not the full array.
const firstWord = myString.split(' ')[0]
String[] newOutput = str.split(" ");
String firstPart = newOutput[0];
I have a string like "test.test.test"...".test" and i need to access last "test" word in this string. Note that the number of "test" in the string is unlimited. if java had a method like php explode function, everything was right, but... . I think splitting from end of string, can solve my problem.
Is there any way to specify direction for split method?
I know one solution for this problem can be like this:
String parts[] = fileName.split(".");
//for all parts, while a parts contain "." character, split a part...
but i think this bad solution.
Try substring with lastIndexOf method of String:
String str = "almas.test.tst";
System.out.println(str.substring(str.lastIndexOf(".") + 1));
Output:
tst
I think you can use lastIndexOf(String str) method for this purpose.
String str = "test.test.test....test";
int pos = str.lastIndexOf("test");
String result = str.substring(pos);
This question already has answers here:
Replace the last part of a string
(11 answers)
Closed 8 years ago.
Let's say I have a string
string myWord="AAAAA";
I want to replace "AA" with "BB", but only the last occurrence, like so:
"AAABB"
Neither string.replace() nor string.replaceFirst() would do the job.
Is there a string.replaceLast()? And, If not, will there ever be one or is there an alternative also working with regexes?
Find the index of the last occurrence of the substring.
String myWord = "AAAAAasdas";
String toReplace = "AA";
String replacement = "BBB";
int start = myWord.lastIndexOf(toReplace);
Create a StringBuilder (you can just concatenate Strings if you wanted to).
StringBuilder builder = new StringBuilder();
Append the part before the last occurrence.
builder.append(myWord.substring(0, start));
Append the String you want to use as a replacement.
builder.append(replacement);
Append the part after the last occurrence from the original `String.
builder.append(myWord.substring(start + toReplace.length()));
And you're done.
System.out.println(builder);
You can do this:
myWord = myWord.replaceAll("AA$","BB");
$ means at the last.
Just get the last index and do an in place replacement of the expression with what you want to replace.
myWord is the original word sayAABDCAADEF. sourceWord is what you want to replace, say AA
targetWord is what you want to replace it with say BB.
StringBuilder strb=new StringBuilder(myWord);
int index=strb.lastIndexOf(sourceWord);
strb.replace(index,sourceWord.length()+index,targetWord);
return strb.toString();
This is useful when you want to just replace strings with Strings.A better way to do it is to use Pattern matcher and find the last matching index. Take as substring from that index, use the replace function there and then add it back to the original String. This will help you to replace regular expressions as well
String string = "AAAAA";
String reverse = new StringBuffer(string).reverse().toString();
reverse = reverse.replaceFirst(...) // you need to reverse needle and replacement string aswell!
string = new StringBuffer(reverse).reverse().toString();
It seems like there could be a regex answer to this.
I initially was trying to solve this through regex, but could not solve for situations like 'AAAzzA'.
So I came up with this answer below, which can handle both 'AAAAA' and 'AAAzzA'. This may not be the best answer, but I guess it works.
The basic idea is to find the last index of 'AA' occurrence and split string by that index:
String myWord = "AAAAAzzA";
String source = "AA";
String target = "BB";
int lastIndex = -1;
if ((lastIndex = myWord.lastIndexOf(source)) >= 0) {
String f = myWord.substring(0, lastIndex);
String b = myWord.substring(lastIndex + target.length() >= myWord
.length() ? myWord.length() : lastIndex + target.length(),
myWord.length());
myWord = f + target + b;
}
System.out.println(myWord);
myWord=myWord.replaceAll("AA(?!A)","BB");
You can do this with String#subtring
myWord= myWord.substring(0, myWord.length() - 2) + "BB";
I have a string like this :
12
I want to get split to [2] ignoring 1. Is it possible to do so in java?
You can use the split() method to split on a regex input or, better yet, if you know the exact position or character you want to split at (as seems to be the case here), just use substring() combined with indexOf(). Something like:
String substring = string.substring(0, indexOf("2"));
where string is your original String variable..
If you know the exact index,
String str = "12"
String s = str.substring(1,2); // output 2
or
String s = str.substring(0, indexOf("2")); //output 2
or
char[] chars = str.toCharArray();
char c = chars[1]; // output
I have a string like delivery:("D1_0"), how do i get the value inside the quotes alone from it. i.e D1_0 alone from it.
You could use regualr expresion like \"(.*?)\" to find that group, or even better, iterate over your String looking for quote marks " and reading characters inside of them until you find another quote mark. Something similar to this.
Try this
int i = stringvariable.indexOf("(");
int j = stringvariable.indexOf(")");
String output = stringvariable.substring(i+2, j-2);
You will get the required value in output variable.
If your string is constant, in that the beginning of the string will not change, you could use the slice function
In Javascript:
var text='delivery:("D1_0")';
alert(text.slice(11, 15)); //returns "D1_0"
In Java:
String text = "delivery:(\"D1_0\")";
String extract = text.substring(11, 15);
Use:
String str = "delivery:(\"D1_0\")";
String arr[] = str.split("[:\"()]"); //you will get arr[delivery, , , D1_0], choose arr[3]
System.out.println(arr[3]);
"If your String is always in this format you can use
String theString = "delivery:(\"D1_0\")";
String array[] = theString.split("\"");
String result = array[1]; // now result string is D1_0
// Note: array[0] contains the first part(i.e "delivery:(" )
// and array[2] contains the second (i.e ")" )