I have a string say 123dance456 which I need to split into two strings containing the first sub-string before the sub-string dance (i.e. 123) and after the sub-string dance (i.e. 456). I need to find them and hold them in separate string variables, say String firstSubString = 123; and String secondSubString = 456;.
Is there any given String method that does just that?
You can use String.split(String regex). Just do something like this:
String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
Please note that if "dance" occurs more than once in the original string, split() will split on each occurrence -- that's why the return value is an array.
You can do this:
String str = "123dance456";
String substr = "dance";
String before = str.substring(0, str.indexOf(substr));
String after = str.substring(str.indexOf(substr) + substr.length());
Or
String str = "123dance456";
String substr = "dance";
String[] parts = str.split(substr);
String before = parts[0];
String after = parts[1];
It is noteworthy that the second answer not work if we have more than one occurrence of the substring. To that end, if we only want the first one to be recognized, it would be safer to call split with a limit:
String[] parts = str.split(substr, 2);
which ensures that the returned array has at most two elements. Also, since split will interpret its input as a regular expression we have to be wary of invalid regular expression syntax. As such, I would much rather the first solution, since it works irrespective of the composition of the original substring.
To make the first answer more efficient -- as it is my preferred answer -- then, we would need to remember the position of the substring:
final int position = str.indexOf(substr);
if (position >= 0) {
//if the substring does occur within the string, set the values accordingly
before = str.substring(0, position);
after = str.substring(position + substr.length());
} else {
//otherwise, default to the empty string (or some other value)
before = "";
after = "";
}
It always pays to pay attention to these little edge cases.
If you are using commons-lang see StringUtils.substringAfter()
Easiest is to use the split method as the other answers suggest. You can also use a Matcher and a Pattern for a more general approach:
String str = "123dance456";
String splitter = "dance";
Pattern p = Pattern.compile("(.*?)" + splitter + "(.*)");
Matcher m = p.matcher(str);
if (m.matches()) {
firstSubString = m.group(1); // may be empty
secondSubString = m.group(2); // may be empty
} else {
// splitter not found in str
}
String[] data = new String("123dance456").split("dance");
Using the Scanner is a nice alternative:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
It is quite convenient to process the tokens as a stream and simply ask via hasNext() if new tokens are available. Also you get nice conversions from the Scanner, even localised, like:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
System.out.println( scanner.nextDouble() * scanner.nextDouble() );
Related
How to get a array of string like ["#{xxxx}","#{yyyy}"] from a string like "abc#{xxxx}def#{yyyy}ghi" using java?
I'm not good at English so I have to make great effort to express my question.
I want to take the uel expressions out, so I think there may be some methods existing to solve this situation.
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//remove first substring from input
String formattedInput = input.substring(input.indexOf("#"), input.lastIndexOf("}") + 1);
//make a regex that checks for string enclosed in } #{
String regex = "(?<=[}])[A-Za-z]*(?=[#])";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(formattedInput);
//remove the characters between } and #{
if (m.find()) {
formattedInput = formattedInput.replaceAll(regex, "");
}
System.out.println(formattedInput);
}
Input: abc#{xxxx}def#{yyyy}
Output: #{xxxx}#{yyyy}
I am not really sure as to what you are trying to ask because your question was not worded properly, but this code will remove any characters that are not enclosed in the #{} tag. You can then split the resultant string into an array. I hope this helps
I want to replace a string by removing the s in the end
Example
Sticks -> Stick
STiCKs -> STiCK
StICks -> StICK
sticks -> stick
while using the
string.replace("sticks", "stick");
doesn't maintain case as it is case sensitive, so I'm seeking for a better option.
You could use a very simple regex for this mission.
(?i) guarantees that your regex will be treated case insensitive
Demo : (?i)(stick)s
Ideone Java Demo
string.replaceAll("(?i)(stick)s", "$1");
One possible solution is regular expressions:
StringBuffer sb = new StringBuffer();
Matcher matcher = Pattern.compile("(stick)s", Pattern.CASE_INSENSITIVE) .matcher(inputString);
while(matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1));
}
matcher.appendTail(sb);
String outputString = sb.toString();
Edit: this is more or less what does String::replaceAll, but replaceAll doesn't give a case insensitive option.
If you just need to remove the 's' at the end of the String you can simply use substring method like this:
String myString = "sTiCks";
myString = myString.substring(0, myString.length()-1);
// Result "sTiCk"
If you need to remove a char or String from your String not knowing where this part will be, you can try something this:
String myString = "sTiCks";
// Part you want to delete
String stringToDelete = "Ck";
// Find where that part starts inside your String
int index = myString.indexOf(stringToDelete);
// If found, use substring method to take only what is before and after that part
if (index >= 0)
myString = myString.substring(0, index) + myString.substring(index + stringToDelete.length(), myString.length());
// Result "sTis"
This will delete the desired part only the first time it finds it. But if the part you want to delete appears more than once in your String you can modify the code to this:
String myString = "sTiCks";
// Part you want to delete
String stringToDelete = "s";
int index;
while ((index = myString.indexOf(stringToDelete)) >= 0)
myString = myString.substring(0, index) + myString.substring(index + stringToDelete.length(), myString.length());
// Result "TiCk"
I hope one of these solutions fits your case.
I don't really get why all the answers so far are so complex. You can just check the last character and if it's a s (or S) you use String#substring (documentation) and leave out the last character:
String text = "STiCks";
char lastCharacter = text.charAt(text.length() - 1);
if (lastCharacter == 'S' || lastCharacter == 's') {
text = text.substring(0, text.length() - 1);
}
If you want to apply that method to multiple words, for example in a sentence, tokenize the sentence first. Then apply the method to each word and rebuild the sentence.
String sentence = StiCks are nice, I like sticks"
String[] words = sentence.split(" ");
StringJoiner joiner = new StringJoiner(" ");
for (String word : words) {
joiner.add(removePluralSuffix(word));
}
String result = joiner.toString();
or the same with Streams:
String result = Arrays.stream(sentence.split(" "))
.map(this::removePluralSuffix)
.collect(Collectors.joining(" "));
Title seems to be simple. But I don't get a good Idea. This is the situation
I have String like this in my Java program
String scz="3282E81WHT-22/24";
I want to split the above string into 3 Strings, such that
first string value should be 3282e81,
Next string should be WHT(ie, the String part of above string and this part is Always of 3 Characters ),
Next String value should be 22/24 (Which will always occur after -)
In short
String first= /* do some expression on scz And value should be "3282e81" */;
String second= /* do some expression on scz And value should be "WHT" */;
String third= /* do some expression on scz And value should be "22/24" */;
Input can also be like
scz="324P25BLK-12";
So 324P25 will be first String, BLK will be second (of 3 Characters). 12 will be third ( After - symbol )
How to solve this?
You can use a regex like this (\d+[A-Z]\d+)([A-Z]+)-([/\d]+) and using Matcher.group(int) method you can get your string splitted into three groups.
Code snippet
String str = "3282E81WHT-22/24";
//str = "324P25BLK-12";
Pattern pattern = Pattern.compile("(\\d+[A-Z]\\d+)([A-Z]+)-([/\\d]+)");
Matcher match = pattern.matcher(str);
System.out.println(match.matches());
System.out.println(match.group(1));
System.out.println(match.group(2));
System.out.println(match.group(3));
Output
true
3282E81
WHT
22/24
Use this to split the entire string in to two
String[] parts = issueField.split("-");
String first = parts[0];
String second= parts[1];
Use this to split the first string into two
if (first!=null && first.length()>=3){
String lastThree=first.substring(first.length()-3);
}
if your String's Second part (WHT) etc will always be of 3 Characters then following code will surely help you
String scz = "3282E81WHT-22/24";
String Third[] = scz.split("-");
String rev = new StringBuilder(Third[0]).reverse().toString();
String Second=rev.substring(0,3);
String First=rev.substring(3,rev.length());
// here Reverse your String Back to Original
First=new StringBuilder(First).reverse().toString();
Second=new StringBuilder(Second).reverse().toString();
System.out.println(First + " " + Second + " " + Third[1]);
You can use subString() method to get this goals.
subString has numbers of overloads.
for first string
String first=scz.subString(0,6);
String second=scz.subString(7,9);
You can use following regex to take out the above type string:
\d+[A-Z]\d{2}|[A-Z]{3}|(?<=-)[\d/]+
In Java, you can use above regex in following way:
Pattern pattern = Pattern.compile("\\d+[A-Z]\\d{2}|[A-Z]{3}|(?<=-)[\\d/]+");
Matcher matcher = pattern.matcher("3282E81WHT-22/24");
while (matcher.find()) {
System.out.println(matcher.group());
}
Output:
3282E81
WHT
22/24
You could us a char array instead of a string so you can access specific characters withing the array.
Example
char scz[] = "3282E81WHT-22/24";
and access the separate characters just by specifying the place in which the array you want to use.
You can try this
String scz="3282E81WHT-22/24";
String[] arr=scz.split("-");
System.out.println("first: "+arr[0].substring(0,7));
System.out.println("second: "+arr[0].substring(7,10));
System.out.println("third: "+arr[1])
Check out my solution -
class Main {
public static void main(String[] args) {
String first = "";
String second = "";
String third = "";
String scz="3282E81WHT-22/24";
String[] portions = scz.split("-");
if (portions.length > 1) {
third = portions[1];
}
String[] anotherPortions = portions[0].split("[a-zA-Z]+$");
if (anotherPortions.length > 0) {
first = anotherPortions[0];
}
second = portions[0].substring(first.length());
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
}
Live Demo.
String scz="3282E81WHT-22/24";
String[] array = scz.split("-");
String str1 = (String) array[0].subSequence(0, 7);
String str2 = array[0].substring(7);
Then the split will be in this order :)
str1
str2
array[1]
if the length of string is fixed for scz, first,second and third the you can use
String first=scz.subString(0,6);
String second=scz.subString(7,9);
String third=scz.subString(10,scz.length());
I want to split the string say [AO_12345678, Real Estate] into AO_12345678 and Real Estate
how can I do this in Java using regex?
main issue m facing is in avoiding "[" and "]"
please help
Does it really have to be regex?
if not:
String s = "[AO_12345678, Real Estate]";
String[] split = s.substring(1, s.length()-1).split(", ");
I'd go the pragmatic way:
String org = "[AO_12345678, Real Estate]";
String plain = null;
if(org.startsWith("[") {
if(org.endsWith("]") {
plain = org.subString(1, org.length());
} else {
plain = org.subString(1, org.length() + 1);
}
}
String[] result = org.split(",");
If the string is always surrounded with '[]' you can just substring it without checking.
One easy way, assuming the format of all your inputs is consistent, is to ignore regex altogether and just split it. Something like the following would work:
String[] parts = input.split(","); // parts is ["[AO_12345678", "Real Estate]"]
String firstWithoutBrace = parts[0].substring(1);
String secondWithoutBrace = parts[1].substring(0, parts[1].length() - 1);
String first = firstWithoutBrace.trim();
String second = secondWithoutBrace.trim();
Of course you can tailor this as you wish - you might want to check whether the braces are present before removing them, for example. Or you might want to keep any spaces before the comma as part of the first string. This should give you a basis to modify to your specific requirements however.
And in a simple case like this I'd much prefer code like the above to a regex that extracted the two strings - I consider the former much clearer!
you can also use StringTokenizer. Here is the code:
String str="[AO_12345678, Real Estate]"
StringTokenizer st=new StringTokenizer(str,"[],",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
s1=AO_12345678
s1=Real Estate
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
Another option using regular expressions (RE) capturing groups:
private static void extract(String text) {
Pattern pattern = Pattern.compile("\\[(.*),\\s*(.*)\\]");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) { // or .matches for matching the whole text
String id = matcher.group(1);
String name = matcher.group(2);
// do something with id and name
System.out.printf("ID: %s%nName: %s%n", id, name);
}
}
If speed/memory is a concern, the RE can be optimized to (using Possessive quantifiers instead of Greedy ones)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"
I need to replace a word in a string looking like "duh duh something else duh". I only need to replace the second "duh", but the first and the last ones need to stay untouched, so replace() and replaceFirst() don't work. Is there a method like replaceFirst(String regex, String replacement, int offset) that would replace the first occurrence of replacement starting from offset, or maybe you'd recommend some other way of solving this?
Thanks!
What about something like this:
String replaceFirstFrom(String str, int from, String regex, String replacement)
{
String prefix = str.substring(0, from);
String rest = str.substring(from);
rest = rest.replaceFirst(regex, replacement);
return prefix+rest;
}
// or
s.substring(0,start) + s.substring(start).replaceFirst(search, replace);
just 1 line of code ... not a whole method.
Will something like this work?
System.out.println(
"1 duh 2 duh duh 3 duh"
.replaceFirst("(duh.*?)duh", "$1bleh")
); // prints "1 duh 2 bleh duh 3 duh"
If you just want to replace the second occurrence of a pattern in a string, you really don't need this "starting from" index calculation.
As a bonus, if you want to replace every other duh (i.e. second, fourth, sixth, etc), then just invoke replaceAll instead of replaceFirst.
An alternative using Matcher:
String input = "duh duh something else duh";
Pattern p = Pattern.compile("duh");
Matcher m = p.matcher(input);
int startIndex = 4;
String output;
if (m.find(startIndex)) {
StringBuffer sb = new StringBuffer();
m.appendReplacement(sb, "dog");
m.appendTail(sb);
output = sb.toString();
} else {
output = input;
}