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 8 years ago.
Improve this question
I have sentences like:
Slno:0 ahdhajdhjahdjahjdhahd <>
Slno:1 ahdhajdhjahdjahjdhahd <>
Slno:2 ahdhajdhjahdjahjdhahd <>
I want to compare with the first 5 charters as "Slno:x" where x is a integer.
if that condition is met I want to print the rest of the lines. and the remove the last <>
so the output looks like:
ahdhajdhjahdjahjdhahd
ahdhajdhjahdjahjdhahd
ahdhajdhjahdjahjdhahd
I tried doing:
if string1.charAt(1)=='S' for all charcaters and than printed from string1[5] to end.
if that conditions are true. Looking for a more better logic
If i understand your requirement correctly i think this would work.try this:
public static void main(string..args) {
String s1= "Slno:0 ahdhajdhjahdjahjdhahd <>";
String[] sSplit = s1.split("\\s");
String f[]= sSplit[0].split(":");
;
if(f[0].equals("Slno") && checkInt(f[1])) {
System.out.println(sSplit[1]);
}
}
public static boolean checkInt(String i) {
try {
Integer.parseInt(i);
return true;
}
catch(Exception ex){
return false;
}
}
You can use Regex and substring.. if you spaces between your sentences: -
String s1 = "Slno:0 ahdhajdhjahdjahjdhahd <>";
if (s1.substring(0, 6).matches("Slno:\\d")) {
System.out.println(s1.substring(7, s1.length() - 3));
}
Output: -
ahdhajdhjahdjahjdhahd
You can try this,
Example: "Slno:0 ahdhajdhjahdjahjdhahd <>"
Split the Sentence to three parts using split method setting delimiters as space.
StringOne = "Slno:0"
StringTwo = "ahdhajdhjahdjahjdhahd"
StringThree = "<>"
Use contains method to match the input. If the given input matches the first part of the string then print the second part of the string.
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 got a java question which is Given a string, return the string made of its first two chars, so the String "Hello" yields "He".
If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "".
Note that str.length() returns the length of a string.
public String firstTwo(String str) {
if(str.length()<2){
return str;
}
else{
return str.substring(0,2);
}
}
I'm wondering is there any other way can solve this question?
Your code looks great! If you wanted to make it shorter you could use the ternary operator:
public String firstTwo(String str) {
return str.length() < 2 ? str : str.substring(0, 2);
}
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Have a scenario to strip the below pattern strings -
abc|hjdj|kleygag|0|0|0|0|
ghys|jkugb|0|0|0
yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|
Looking for help to get a regex in java to separate these values excluding everything from the first occurence of pipe before 0, the output should look like-
abc|hjdj|kleygag
ghyd|jkugb
yuubf|kluygb|tyrffv|nutgv
Just need regex, which has been answered and been very helpful, would just be very helpful to know just the regex and not the complete code for the reverse pattern as requested in the followup question
Is regex necessary for this? substring() gets you want you want easily.
Update
I saw a comment where you're also wanting a case where the data looks like, "0|0|0|0|abdc|ghyft|rtyu". I've modified my answer to account for that case and a case where the data could be, "0|0|0|0|abdc|ghyft|rtyu|0|0|0|"
Either way:
public static void main(String[] args) throws Exception {
List<String> strings = new ArrayList(){
{
add("abc|hjdj|kleygag|0|0|0|0|");
add("ghys|jkugb|0|0|0");
add("yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|");
add("0|0|0|0|abdc|ghyft|rtyu");
add("0|0|0|0|abdc|ghyft|rtyu|0|0|0|0|0|");
}
};
// Non Regex
System.out.println("Non Regex");
for (String string : strings) {
int startIndex = -1;
int endIndex = -1;
// Find first non zero character
for (int i = 0; i < string.length(); i++) {
if ('a' <= string.charAt(i) && string.charAt(i) <= 'z') {
startIndex = i;
break;
}
}
// Find first pipe zero |0 after startIndex
endIndex = string.indexOf("|0", startIndex);
// Determine which substring() to use based on the endIndex results
System.out.println(endIndex > -1 ? string.substring(startIndex, endIndex) : string.substring(startIndex));
}
System.out.println("");
// Regex
System.out.println("Regex");
for (String string : strings) {
System.out.println(string.replaceAll("\\|0|0\\||\\|$", ""));
}
}
Results:
Non Regex
abc|hjdj|kleygag
ghys|jkugb
yuubf|kluygb|tyrffv|nutgv
abdc|ghyft|rtyu
abdc|ghyft|rtyu
Regex
abc|hjdj|kleygag
ghys|jkugb
yuubf|kluygb|tyrffv|nutgv
abdc|ghyft|rtyu
abdc|ghyft|rtyu
You can use a regex like this:
\|0|0\||\|$
Java code:
String regex = "\\|0|0\\|\\|$";
System.out.println("abc|hjdj|kleygag|0|0|0|0|".replaceAll(regex, ""));
System.out.println("ghys|jkugb|0|0|0".replaceAll(regex, ""));
System.out.println("yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|".replaceAll(regex, ""));
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 8 years ago.
Improve this question
I have this string I got from a table via 'string.split(" ");' the problem I have is one of them should include a period. How would I go about searching for this period. I do know I have to search for it because I code in Lua. Though we used String.find() methods.
What is the way I would remove a period from a string in a String[] table?
Thanks in advance :)
This is for school.
If I understand you correctly, you have a String that you've split by delimiting on a space. You now want to search for a period in the resultant array of Strings.
for(String s : stringArray) {
if (s.contains(".")) {
//do something
}
}
Unfortunately, I'm not that clever with RegExp, but you could...
String s = "This.is.a.test";
while (s.contains(".")) {
s = s.replace(".", "");
}
System.out.println(s);
I'm sure there's a wonderful single line RegExp to do the same thing
Updated based on comments
Because String is not mutable, you will need to reassign it back to the original array
String[] apples = {"one", "two.", "three"};
for (int index = 0; index < apples.length; index++) {
String s = apples[index];
while (s.contains(".")) {
s = s.replace(".", "");
}
apples[index] = s;
}
for (String s : apples) {
System.out.println(s);
}
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 8 years ago.
Improve this question
Hi i want to do some validations.I used to put regex in JS but im new to regex in java, so i tried to make up a code on similar lines in java.
Here is what i did.
1)Check whether first character in string is alphanumeric.
2)Check whether the string atleast 1 number.
so i wrote a code, but it is always returning false.I am not sure if i'm doing this correctly.
private static boolean checkEmbeddedPassword(final String field) {
boolean returnValue=true;
String testpatternAlpha="/^[A-Za-z0-9].+$/";
String testNumber="/[0-9]/";
Pattern pattern=Pattern.compile(testpatternAlpha);
Pattern pattern2=Pattern.compile(testNumber);
Matcher matcher = pattern.matcher(field);
Matcher matcher2 = pattern2.matcher(field);
boolean firstChar=matcher.matches();
boolean numberFlag=matcher2.matches();
System.out.println("-----the value of pwd iss-----"+field);
System.out.println("---------Regex---------Out--put-----"+firstChar);
System.out.println("---------Regex---------Out- for numeral-put-----"+numberFlag);
if(firstChar){
returnValue=false;
}
else if(field.contains(" "))
{
System.out.println("-----------cannot have space------");
returnValue=false;
}
else if(numberFlag)
{
returnValue=false;
}
return returnValue;
}
You don't need the / prefix and suffix in Java:
String testpatternAlpha="^[A-Za-z0-9].+$";
Also in your situation you'll want to be using Matcher.find rather than Matcher.matches. You can read why in the API documentation: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html
we do not use / on the beginning and end of the regex in java.
try this...
String testpatternAlpha="^[A-Za-z0-9].+$"; // first character in string is alphanumeric
String testNumber="[0-9]"; //test single number