I have a string, for ex:
There exists a word *random*.
random will be a random word.
How can I use a regular expression to replace every character of random with * and have this result:
There exists a word ********.
So the * replaces every character, in this case 6 characters.
Notice that I am after to replace only the word random, not the surroundings *.
So far I have:
str.replaceAll("(\\*)[^.]*(\\*)", "\\*");
But it replaces *random* with *, instead of the desired ******** (total of 8).
Any help, really appreciated...
If you have just a single word like that: -
As far as current example is concerned, if you are having just a single word like that, then you can save yourself from regex, by using some String class methods: -
String str = "There exists a word *random*.";
int index1 = str.indexOf("*");
int index2 = str.indexOf("*", index1 + 1);
int length = index2 - index1 - 1; // Get length of `random`
StringBuilder builder = new StringBuilder();
// Append part till start of "random"
builder.append(str.substring(0, index1 + 1));
// Append * of length "random".length()
for (int i = 0; i < length; i++) {
builder.append("*");
}
// Append part after "random"
builder.append(str.substring(index2));
str = builder.toString();
If you can have multiple words like that: -
For that, here's a regex solution (This is where it starts getting a little complex): -
String str = "There exists a word *random*.";
str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
System.out.println(str);
The above pattern replaces all the characters that is not followed by string containing even numbers of * till the end, with a *.
Whichever is appropriate for you, you can use.
I'll add an explanation of the above regex: -
(?<! ) // Not preceded by a space - To avoid replacing first `*`
. // Match any character
(?! // Not Followed by (Following pattern matches any string containing even number of stars. Hence negative look-ahead
[^*]* // 0 or more Non-Star character
[*] // A single `star`
[^*]* // 0 or more Non-star character
[*] // A single `star`
)* // 0 or more repetition of the previous pattern.
[^*]*$ // 0 or more non-star character till the end.
Now the above pattern will match only those words, which are inside a pair of stars. Provided you don't have any unbalanced stars.
You can extract the word between * and do a replaceAll characters with * on it.
import java.util.regex.*;
String txt = "There exists a word *random*.";
// extract the word
Matcher m = Pattern.compile("[*](.*?)[*]").matcher(txt);
if (m.find()) {
// group(0): *random*
// group(1): random
System.out.println("->> " + m.group(0));
txt = txt.replace(m.group(0), m.group(1).replaceAll(".", "*"));
}
System.out.println("-> " + txt);
You can see it on ideone: http://ideone.com/VZ7uMT
try
String s = "There exists a word *random*.";
s = s.replaceAll("\\*.+\\*", s.replaceAll(".*(\\*.+\\*).*", "$1").replaceAll(".", "*"));
System.out.println(s);
output
There exists a word ********.
public static void main(String[] args) {
String str = "There exists a word *random*.";
Pattern p = Pattern.compile("(\\*)[^.]*(\\*)");
java.util.regex.Matcher m = p.matcher(str);
String s = "";
if (m.find())
s = m.group();
int index = str.indexOf(s);
String copy = str;
str = str.substring(0, index);
for (int i = index; i < index + s.length(); i++) {
str = str + "*";
}
str = str + copy.substring(index + s.length(), copy.length());
System.out.println(str);
}
Related
I have an Array of Characters ' A ' and ' B '
Whats a smart way to count the ' Runs '
example :
AABBBAAAABBAB
should be 6 because there are 6 runs as seen below.
1 2 3 4 5 6
AA BBB AAAA BB A B
tried something like:
if (!(sortedRuns.get(i) == sortedRuns.get(i+1))) {
runsAmount++;
}
but obviously run into 'out of bound problems'
Issue
e.g char array A A B B C C
array positions 0 1 2 3 4 5
when you reach at the position 5 then sortedRuns.get(i+1) mean 5+1=6 which doesn't exist hence the exception
Solution
1.) Traverse the array
2.) Increment run if char changes and assign new char to temp char
String s="AABBBAAAABBAB";
int run=1;
// fetch first char
char temp=s.charAt(0);
// traverse char array
for (char ch : s.toCharArray()) {
// assign value of new char to temp and increment run
// when value of char changes
if (ch!=temp) {
run++;
temp=ch;
}
}
System.out.println(run);
Output:
6
I would use an auxiliar variable to save the last character. Then increment when a new character is different from my auxiliar variable, then update this variable. Easy and not out of bounds exceptions.
u can try this easy one-liner:
public static void main(String[] args) {
String s="AABBBAAAABBAB";
int charCount = s.length() - s.replaceAll("A", "").length();
System.out.println(charCount);
System.out.println(s);
}
You could use a regular expression.
/(.)\1*/ gives 6 matches.
(.) matches any character.
\1* then matches as many of the first character as possible.
Java Example:
final String regex = "(.)\\1*";
final String string = "AABBBAAAABBAB";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) {
count ++;
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
System.out.println("Match count: " + count);
This has the advantage of working with any character string.
My Requirement is like , split the given string into sub strings of charter and numeric values.
And the input value will always begin charter only.
Input : String strValue = "ABCD12345";
Out put:
A1 = ABCD
A2 = 12345
You need to use lookaround assertions.
String s = "ABCD12345";
String parts[] = s.split("(?<=[A-Za-z])(?=\\d)");
System.out.println(Arrays.toString(parts));
(?<=[A-Za-z])(?=\\d) regex would match the boundary which exists between alphabets and digits. Splitting according to the matched boundary will ive you the desired output.
Output:
[ABCD, 12345]
There's probably some mistakes and probably not the most efficient here but here's a vauge shot. Feedback welcome of course.
var char;
var = num;
str = "somestring23498";
for(i=1:1=str.length - 1;i++){
if ((typeof str[i] + 1) == "string"){
char = char + str[i];
} else if((typeof str[i] + 1) == "integer"){
num = num + str[i];
}
}
You can alternatively create a char buffer with space or a delimeter between char boundary.
String str = "ABCD1234cda209ad";
char[] chars = new char[2*str.length()];
int i=0;
boolean isPrevcharANum = false;
for(char c:str.toCharArray()){
if(Character.isDigit(c)){
if(!isPrevcharANum){
chars[++i] = ' ';
}
isPrevcharANum = true;
}else{
if(isPrevcharANum){
chars[++i] = ' ';
isPrevcharANum = false;
}
}
chars[++i] = c;
}
System.out.println("Output : "+new String(chars));
Output : ABCD 1234 cda 209 ad
again with regex but shorter
String parts[] = s.split("(?<=\\D)(?=\\d)"); // [ABCD, 12345]
Internally, split() uses a Pattern to find the place where to split. The first part in braces is a positive lookbehind.
\D is the oposite to \d and means each character but a digit.
The second part in braces is a positive lookahead \d which means here must be a digit.
So a split is made when to the left of the position is any character but a digit and at the same time there is a digit in this position.
In a program where currency is input in the form £2 or 10p, for example, is there a method to split this into two variables in the form
currencyType = £
currencyValue = 2
or
currencyType = p
currencyValue = 10
where currencyType is a string and currencyValue is an int?
An idea for a solution without regular expressions, although I'd prefer one of those:
String entry = "€2.73";
StringBuilder currency = new StringBuilder();
StringBuilder value = new StringBuilder();
for (char c : entry.toCharArray()) {
if (Character.isDigit(c) || c == '.' || c == ',') {
value.append(c);
} else {
currency.append(c);
}
}
System.out.println("Value = " + value + " Currency = " + currency);
Use patten and matcher classes like below. \d+ matches one or more digits where \D+ matches one or more non-digit characters.
String s1 = "£2";
Matcher m = Pattern.compile("(\\D+)|(\\d+)").matcher(s1);
while(m.find())
{
if (m.group(1) != null)
System.out.println("Currency Type: " + m.group(1));
if (m.group(2) != null)
System.out.println("Currency Value: " + m.group(2));
}
Output:
Currency Type: £
Currency Value: 2
OR
Use this regex, if you want to deal also with the decimal value.
Pattern.compile("(\\D+)|(\\d+(?:\\.\\d+)?)");
DEMO
You can use this regular expression to get your result: "(.*?)([\\d,]*)(.*?)"
This will split the input into three groups:
1) Leading currency token
2) Value token (can contain a ',', in the string version you can replaceAll ',' with '' and then convert to integer)
3) Trailing currency token
By looking at the groups from the regex, you can figure out if the leading or trailing currency is present and then get the value from the second group. You can write the code yourself by looking up usage for java regex.
You can input your value as a string, split it normally with the split function and assign each value to its own string. Then convert the string to an integer.
int currencyValue = Integer.parseInt(array[0]);
String currencyType = array[1];
array[] is the array that you split the string into.
String input = user_input.nextLine();
char[] array = input.toCharArray();
for(int i = 0; i < input.length(); i++) {
if (Character.isLetter(array[i])){
//use .split based on the output of the if statement
}
}
I have a string .. I need to search for some particular texts inside this string if it is present total numbers need to be returned.
I know how to return a character of particular place
String string = "Hi How are you";
b = string.contains("H") ;
But I need to get multiple characters for example..I need to search for 'A' and 'S' inside a string and I need number of times it is coming.
how I will write the code for that.I know how to search in a particular place and search for a particular character. But how for more than i character and return its total number.
String string = "Hai How are you!";
char aChar = anotherPalindrome.charAt(9);
I know it will reeturn 9th place character.
String string = "Niagara. O roar again!";
String roar = string.substring(5, 7);
I know it will return 'How'
You have to use regular expressions. Android provides the Pattern class to this kind of things.
You can do like this here three characters AJS can be searched.. if you need to search its small characters give like 'AaJaSs'..
String string;
Pattern pattern = Pattern.compile("([AJS])"); //case insensitive
Matcher matcher = pattern.matcher(String);
int count = 0;
while (matcher.find()) count++;
Try looping with help of the indexOf() method.
Example:
String exampleString = "Hai How are you!";
String searchedString = "H";
int index = exampleString.indexOf(searchedString , 0);
int counter = 0;
while(index != -1) {
counter++;
int index = exampleString.indexOf(searchedString , index);
}
System.out.println("The character " + searchedString + " occurs " + counter + " times.");
You can make a method to count the frequency of the character in the string :
public static int count(char character){
return str.toUpperCase().split((""+character).toUpperCase()).length - 1;
}
Then use this method as shown below:
b = string.count('H');
b = string.count('A');
String lower = Name.toLowerCase();
int a = Name.indexOf(" ",0);
String first = lower.substring(0, a);
String last = lower.substring(a+1);
char f = first.charAt(0);
char l = last.charAt(0);
f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
String newname = last +" "+ first;
System.out.println(newname);
i want to take variables F and L and replace the lowercase first letters in last and first so they will be uppercase. How can I do this? i want to just replace the first letter in the last and first name with the char first and last
if you are trying to do what i think you are, you should consider using the apache commons-lang library, then look at:
WordUtils.capitalize
obviously, this is also open source, so for the best solution to your homework i'd take a look at the source code.
However, if i were writing it from scratch (and optimum performance wasn't the goal) here's how i would approach it:
public String capitalize(String input)
{
// 1. split on the negated 'word' matcher (regular expressions)
String[] words = input.toLowerCase().split("\\W");
StringBuffer end = new StringBuffer();
for (String word : words)
{
if (word.length == 0)
continue;
end.append(" ");
end.append(Character.toUpperCase(word.charAt(0)));
end.append(word.substring(1));
}
// delete the first space character
return end.deleteCharAt(0).toString();
}
While there's more efficient ways of doing this, you almost got it. You'd just need to concatenate the uppercase chars with the first and last name, bar the first character.
String newname = "" + l + last.subString(1) + " " + f + first.subString(1);
EDIT:
You could also use a string tokenizer to get the names as in:
StringTokenizer st = new StringTokenizer(Name);
String fullName = "";
String currentName;
while (st.hasMoreTokens()) {
/* add spaces between each name */
if(fullName != "") fullName += " ";
currentName = st.nextToken();
fullName += currentName.substring(0,0).toUpperCase() + currentName.substring(1);
}
String name = "firstname lastname";
//match with letter in beginning or a letter after a space
Matcher matcher = Pattern.compile("^\\w| \\w").matcher(name);
StringBuffer b=new StringBuffer();
while(matcher.find())
matcher.appendReplacement(b,matcher.group().toUpperCase());
matcher.appendTail(b);
name=b.toString();//Modified Name