If String Is Contained By Brackets, Remove Them [closed] - java

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");

Related

strings in println(); method [closed]

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 8 years ago.
Improve this question
System.out.println("Strings to be printed");
In the above line of code, the strings are coated with double inverted commas
and when we want to print the non-string values or variables, we separate by commas.
Does the compiler ask for the commas? or is it required by the println(); to determine the strings separately?
why is inverted commas required by println();?
It is not so much required by println as required by the Java language.
The println method takes a String argument, and the way to write a String literal in Java is you put double-quote characters around it.
If you didn't put the double-quotes around 'String to be printed' then the Java Language Specification says that you have a sequence of 4 identifiers: String to be printed. That is not a valid Java expression ... and you will get a compilation error.
Why is it that way? Well, it is also consistent with the vast majority of other programming languages ... including Java's direct antecedents ... and that is as good an explanation as you are likely to get.
I suppose you could also be asking why they need to have rules like this: why couldn't the compiler just figure out what the programmer means. And the answer to that is that it is beyond the state of the art ... and probably beyond the realms of possibility. (How does a compiler figure out what the programmer means, when a lot of the time the programmer doesn't know himself!?)
Java: Creating Strings
The most direct way to create a string is to write:
String greeting = "Hello world!";
In this case, "Hello world!" is a string literal—a series of
characters in your code that is enclosed in double quotes.
Whenever it encounters a string literal in your code, the compiler
creates a String object with its value—in this case, Hello world!.
So it's not the requirement of println() but it is a way to create a string literal in Java.
"Strings to be printed"
This is called a String Literal. The compiler understands it as a string. If you want to add a " in the middle of it you use a \ before it to escape the character.
"Strings \"to\" be printed"

Regular Expression to parse category steps [closed]

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);
}

how to remove characters of String from left in java [closed]

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);

What is the equivalent of square brackets for multiple characters? [closed]

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
Regex [abcd] can take any one character out of a, b, c and d.
What if I want to take any one string out of abc, def and ijk.
So something like ["abcd" "def" "efg"] (but this obviously doesn't work).
How would I do this in Java?
your regex could look like this:
(abcd|def|efg)
[] is only for single characters.
You can use | for multiple characters (X|Y means "Either X or Y"):
abcd|def|efg
In case there's anything else in the regex, you'd want to surround it with brackets:
other(abcd|def|efg)stuff
The above matches these strings:
otherabcdstuff
otherdefstuff
otherefgstuff
Where-as:
otherabcd|def|efgstuff
would obviously match these strings:
otherabcd
def
efgstuff

How to write a regular expression to remove all alphaberical characters from a String [closed]

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}]+","");

Categories