I have this String :
String myStr = "something.bad#foo.us"
I want to get just the "something.bad" from myStr ?
You just need to use substring having found the right index to chop at:
int index = myStr.indexOf('#');
// TODO: work out what to do if index == -1
String firstPart = myStr.substring(0, index);
EDIT: Fairly obviously, the above takes the substring before the first #. If you want the substring before the last # you would write:
int index = myStr.lastIndexOf('#');
// TODO: work out what to do if index == -1
String firstPart = myStr.substring(0, index);
You can use
String str = myStr.split("#")[0];
It will split the string in two parts and then you can get the first String element
Related
String add_filter = address.split("\\,", 2)[0];
This removes the text after the first comma. I need to remove the text after the second comma without using the loop.
address.split("\\,")[2];
That splits the string around commas. THe 0th index is before the first comma, the 1st is after the 1st comma, the 2nd is after the 2nd comma, etc. Note that this code assumes there's at least 2 commas. If there aren't, you need to save the array returned by split() and check the length to make sure its 3 or higher. Otherwise there was no second comma.
try following code :
//Finding the Nth occurrence of a substring in a String
public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos + 1);
return pos;
}
then you can remove string after this index position the same as following code :
String newStr = address.substring(0,ordinalIndexOf(address,",",2)- 1)
Try below code
String s = "Hello,world,good bye";
s = s.replaceFirst(",(.*?),.*", " $1");
System.out.println(s);
I try to replace a character and all it's following characters within a string with another character.
This is my code so far.
String name = "Peter Pan";
name = name.replace("er", "abc");
Log.d("Name", name)
the result should be: "Petabc"
I would highly appreciate any help on this matter!
A way to achive your goal:
search the string for the first appearance of the sequnce you want to replace
use that index and cut the string using String#substring
add your replace sequence to the end of the substring you just created
fin.
Good luck.
EDIT
In code it might look like this (not tested)
public static String customReplace(String input, String replace)
{
int index = input.indexOf(replace);
if(index >= 0)
{
return input.substring(index) + replace; //cutting string down to the required part and adding the replace
}
else
return null; //String 'input' doesn't contain String 'replace'
}
You could use a Regular Expression here with String's built-in replaceAll method to very easily do what you want:
original.replaceFirst(toReplace + ".*", replaceWith);
For example:
String original = "testing 123";
String toReplace = "ing";
String replaceWith = "er";
String replaced = original.replaceFirst(toReplace + ".*", replaceWith);
After the above, replaced will be set to "tester".
I have tried a code to replace only specific character. In the string there are three same characters, and I want to replace the second or third character only. For example:
String line = "this.a[i] = i";
In this string there are three 'i' characters. I want to replace the second or third character only. So, the string will be
String line = "this.a[i] = "newChar";
This is my code to read the string and replace it by another string:
String EQ_VAR;
EQ_VAR = getequals(line);
int length = EQ_VAR.length();
if(length == 1){
int gindex = EQ_VAR.indexOf(EQ_VAR);
StringBuilder nsb = new StringBuilder(line);
nsb.replace(gindex, gindex, "New String");
}
The method to get the character:
String getequals(String str){
int startIdx = str.indexOf("=");
int endIdx = str.indexOf(";");
String content = str.substring(startIdx + 1, endIdx);
return content;
}
I just assume that using an index is the best option to replace a specific character. I have tried using String replace but then all 'i' characters are replaced and the result string look like this:
String line = "th'newChar's.a[newChar] = newChar";
Here's one way you could accomplish replacing all occurances except first few:
String str = "This is a text containing many i many iiii = i";
String replacement = "hua";
String toReplace = str.substring(str.indexOf("=")+1, str.length()).trim(); // Yup, gets stuff after "=".
int charsToNotReplace = 1; // Will ignore these number of chars counting from start of string
// First replace all the parts
str = str.replaceAll(toReplace, replacement);
// Then replace "charsToNotReplace" number of occurrences back with original chars.
for(int i = 0; i < charsToNotReplace; i++)
str = str.replaceFirst(replacement, toReplace);
// Just trim from "="
str = str.substring(0, str.indexOf("=")-1);
System.out.println(str);
Result: This huas a text contahuanhuang many hua many huahuahuahua;
You set set charsToNotReplace to however number of first number of chars you want to ignore. For example setting it to 2 will ignore replacing first two occurrences (well, technically).
Lets say, search string is
"Hellothisissanjayhelloiamjavadeveloperhello"
And Search pattern is * Hello* I want to get starting and ending indexes of each matched strings like
first Hello--- start index=0, end = 4,
second Hello-- start index=22, end = 26,
like this
Split the text by space.
Iterate and group the word matching hello with corresponding index.
corresponding index will be "Hello Start index".
4 corresponding index + 3 will be "end".
You don't need a regex for this solution. Just simple String#indexOf method in a while loop will give you start and end index.
String input = "Hellothisissanjayhelloiamjavadeveloperhello";
String kw = "hello";
String len = kw.length();
String pos = 0;
int i;
while ((i=input.indexOf(kw, pos)) >= 0) {
pos = i +len;
System.out.println("Starting index=%d, end=%d\n", i, pos);
}
I have this string,
anyType{image0=images/articles/4_APRIL_BLACK_copy.jpg; image1=images/articles/4_APRIL_COLOR_copy.jpg; }
What i want is only
"images/articles/4_APRIL_BLACK_copy.jpg"
How do i get this?
This is how I perform a split in my app.
String link = "image0=images/articles/4_APRIL_BLACK_copy.jpg";
String[] parts = link.split("=");
String first = parts[0];
Log.v("FIRST", first);
String second = parts[1];
Log.v("SECOND", second);
This method will split your string into 2 at the "=" and give you 2 split strings. In your case, the String second is the result you want.
This should work:
s.split("=")[1]
You are splitting the string on = which would return substrings in an array. The second element is what you need.