Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a String in java Which contains following characters ..
'2010-12-04' and '2013-12-03' and wwid='1234'
Now as per my need i have to remove the starting characters of this and make it like..
and wwid='1234'
I tried it through substring concept where i tried to give the starting point where it needs to be deleted and after that add it into String but i am not able to get it..
Please help me to solve this.
Thanks in advance
You can first find the last Indexof String and, and make it as start position to do substring method.
Try
String text = "'2010-12-04' and '2013-12-03' and wwid='1234'";
text = text.substring(text.lastIndexOf("and"));
System.out.println(text);
Output in console:
and wwid='1234'
Combine IndexOf() and Sustring() or use replace(UndesiredCharSequence, "")
Actually substring should work. One thing you need to consider is that substring does not change the actual string but returns a new string. So use:
String newString = str.substring(30);
You could try this:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
String myNewString = input.substring(numberOfPositionsToRemove);
In your case it would be:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
String myNewString = input.substring(30);
Or to make it more dynamic you could use:
String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
int index = input.lastIndexOf("and");
String myNewString = input.substring(index);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get a character index in a java.lang.String. So, for example, I want to find the '2' character index in the "0123456789" string (the index will be 2). There is any method in the String class to use for this, or a simple code?
Thanks for the answer!
You can iterate through the String and look for a character you seek.
Just use a for loop like this:
String test = "I want to test something";
for(int i=0;i<test.length;i++) {
char t = test.charAt(i);
// do something with char
}
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt%28int%29
index will be -1 if 'a' is not in this string.
Note this code will only find the first 'a' in this string. If you need to find multiple locations of a character, then loop through the string using solution in another answer.
String str = "there is a letter";
int index = str.indexOf('a');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this String
English>Arts>Photography
Want regular expression to delete all after last (>) ,Want output to be like this
English>Arts>
Many Thanks,
use String.lastIndexOf() instead of String.indexOf(). no need to go to regexps for this
Use LastIndexOf as mentioned below :
Substring(0, [YourString].LastIndexOf('>'));
You can use substring and lastIndexOf as shown below:
String s = "English>Arts>Photography";
System.out.println( s.substring(0,s.lastIndexOf('>') ));
If you really didn't want to use .lastIndexOf() (i don't know why you wouldn't). The regex would have been:
String input = "English>Arts>Photography";
Pattern p = Pattern.compile("(.*>)[A-Za-z]+");
Matcher m = p.mathcer(input);
if (m.matches()) {
String output = m.group(1);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having an activity that return string message
String value of my file id like
file!^#123456
I am interesting only the number of that file id i see some regex expression but i did not understand properly.
if I get it right regex for "I am interesting only the number of that file id" will be : [0-9]+ or with Java Predefined Character Classes; [\d]+.
Try this.
String str = "file!^#123456";
System.out.println(str.replaceAll("\\D+",""));
You could go at it using substring on it's own:
String s = "file!^#123456";
System.out.println(s.substring(s.indexOf("#") + 1, s.length()));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a function that will take input, check to see if it has "[" as the first char and "]" as the last and remove them if it does. The string may have more brackets in them, if that's the case then I need to print an error statement. Everywhere I've looked, I've seen examples of how to do this if it were just the two brackets, but I need to check if there are any more as well, and that's where I'm getting confused.
I've seen examples of using regex, and I know that if I can find a string with just the two brackets on the outside, I can use substring to trim it, but I can't figure out how to check the string for the instances of brackets without remvoing all of them.
Help would be greatly appreciated. Thanks.
if(str.chatAt(0) == '[' && str.charAt(str.length()-1) == ']') //str.matches("\\[.*\\]")
{
str = str.substring(1, str.length() - 1);
}
if(str.contains("[") || str.contains("]")) //str.matches(".*[\\[\\]].*")
{
//throw your error
}
Posted potential regex solutions beside the if statements. Warning: Not the greatest at regex, so they may not be correct.
as #Ingo pointed out in the comments, str.charAt(0) will fail on empty string.
You can use String.charAt(int) for do it:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#charAt%28int%29
Since you claim to know what you are doing to trim the leading and trailing brackets, it sounds like the missing link is the .contains() method on the String class.
if (myString.contains("]") || myString.contains("["))
throw new Exception("myString contains invalid brackets");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to remove all alphabetical characters from a string usign a regular expression in java/android?
val = val.replaceAll("/A/z","");
Try this:
replaceAll("[a-z]", "");
Also have a look here:
Replace all characters not in range (Java String)
This will remove all alphabetical characters
String text = "gdgddfgdfh123.0114cc";
String numOnly = text.replaceAll("\\p{Alpha}","");
Have a look into Unicode properites:
\p{L} any kind of letter from any language
So your regex would look like this
val = val.replaceAll("\\p{L}+","");
To remove also combined letters use a character class and add \p{M}
\p{M} a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)
Then you end here:
val = val.replaceAll("[\\p{L}\\p{M}]+","");