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 1 year ago.
Improve this question
Saying that there are String A = "aabbccdd" and String B = "abcd",
is there any way to remove the matching characters of String B towards String A for only one time?
Expected output is A = "abcd".
I know it will solve the problem when using for loops, but is there any simpler way to do it?
For example, using replaceAll or regular expressions?
you can use distinct() method
public static void main(String[] args) {
String str = "aabbccdd";
String result = str.chars().distinct().boxed()
.map(c -> (char) (c.intValue()))
.map(String::valueOf)
.collect(Collectors.joining());
System.out.println(result);
}
you can use the regex for that
A = A.replaceAll("([a-z]+)\1","");
can find out more about regex here https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
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 7 years ago.
Improve this question
It's about string to make a compact output.
Example 1
Input : boooooob
Output : bob
Example2
Input : boobaabbiibbuuuuub
Output : bobabibub
Can anyone help me?
I'm stuck, thx.
This can be solved by using regular expression (\\w)\\1+
public class RemoveReplicateLetter {
public static void main(String[] args) {
//For input: boooooob
System.out.println(removeReplicateLetter("boooooob"));
//For input: boobaabbiibbuuuuub
System.out.println(removeReplicateLetter("boobaabbiibbuuuuub"));
}
public static String removeReplicateLetter(String word) {
/*
REGEX:
(\\w)\\1+
- \\w : matches any word character (letter, digit, or underscore)
- \\1+ : matches whatever was in the first set of parentheses, one or more times.
*/
return word.replaceAll("(\\w)\\1+", "$1");
//Here $1 means return letter with match in word by regex.
}
}
Output:
bob
bobabibub
This method should do the job:
public String simplify(String input) {
// Convert to an array for char based comparison
char[] inputArray = input.toCharArray();
// First char will always be included in the output because there is no char to compete
String output = String.valueOf(inputArray[0]);
// Check every char against the following
for (int i = 1; i < inputArray.length; i++) {
// If not equal
if (inputArray[i - 1] != inputArray[i]) {
// Add to output
output += inputArray[i];
}
}
// Return the result
return output;
}
It will compare every char with the following one and only adds it to the output if they are not equal.
Note: This is just a proof of concept, not an optimal solution.
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've got this string:
"type":"image","originX":"center","originY":"center","left":135,"top":259,"width":270,"height":519,"fill":"rgb(0,0,0)","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"src":"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg","filters":[]},
each part is seperated by a comma, i want to be able to extract a few of the numbers into doubles. The ones i want are left, top, scaleX, scaleY and angle. How shall i approch this?
thanks
If you don't want to rely on using JSON parsers (you should, though, if you are using JSON a lot), you could use the split-method on the entire string and split according to , (comma), find the chunks of data that you want, split those according to : and read the data directly from the 2nd slot in the resulting array.
You may need to substring the last " to be able to parse the numbers directly, though.
But like I said, you really do want to use a JSON parser of some kind if you are using JSON more than a few times in your program.
Code example:
String abc = "ABC:123,DEF:456,GHI:789";
String[] chucks = abc.split(",");
String[] oneToThree = chunks[0].split(":");
String nums = oneToThree[1];
System.out.println(nums);
//This will print 123
I know that someone already replied, but I've been doing this, hope that help too:
public class HelloWorld{
public static void main(String []args){
String text ="\"type\":\"image\",\"originX\":\"center\",\"originY\":\"center\",\"left\":135,\"top\":259,\"width\":270,\"height\":519,\"fill\":\"rgb(0,0,0)\",\"overlayFill\":null,\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"src\":\"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg\"";
//Just left and scaleX for example
String left = readValue(text, "left");
String scaleX = readValue(text, "scaleX");
System.out.println("left:" + left);
System.out.println("scaleX:" + scaleX);
}
public static String readValue(String text, String key)
{
//search for the init of the value
int start = text.indexOf("\"" + key + "\"");
//search for the end of the value
int end = text.indexOf(",", start + key.length() + 3);
//return the value. these + 3 , is for quotes and ":"
return text.substring(start + key.length() + 3,end);
}
}
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 extract email address from String like below with delimiter as "AND" ?
String str="abb#AND.comANDbbb.comANDccc#xxd.AND";
Try with following code segment,
public class TokenizerTest
{
private final String testStr = "abb#AND.comANDbbb.comANDccc#xxd.AND";
public static void main(String[] args)
{
TokenizerTest tst = new TokenizerTest();
tst.tokenize();
}
public void tokenize()
{
if(testStr != null)
{
Pattern p = Pattern.compile("[*]*AND*[^.AND]");
String[] tokens = testStr.split(p.pattern());
for(String token:tokens)
{
System.out.println(token);
}
}
}
}
It results in
abb#AND.com
bb.com
cc#xxd.AND
Short answer: Computer says no.
Based on the string you provided, it can probably not be done in a reliable way, since it is not clear what is an email address, and what is a delimiter. In fact, it is difficult to extract more than one or two valid email-addresses at all from that line.
I recommend you first check your dataset to see if it is corrupted, then find a way to use a different delimiter.
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 containing this:
D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo.
I want to get just this part:
wt\lifecycle\StateRB
How can I do that?
You can simply spilt whole path to parts and then get the parts you want.
String path = "D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo";
String[] parts = path.split("\\");
parts = Arrays.copyOfRange(parts, parts.length-3, parts.length);
Or you can get throught string using loop (this seems to be better)
int index = 0, i = 0;
Stack<String> al = new Stack<String>();
while((index = path.lastIndexOf()))!=-1 && i < 3) {
al.push((path = path.substring(index)));
i++;
}
String[] parts = (String[])al.toArray(); //if you don't have array elements
// in correct order, you can use
// Collections.reverse with Arrays.asList
// applied on array
You can use string tokeniezer with \ delimiter and fetch only last three string tokens. i hope that above path going to be constant always.
http://www.tutorialspoint.com/java/java_string_substring.htm
Check the above link
example :
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );