Java Replace substring in String with exact number of character - java

My string is "test"
"test" has 4 characters
I want to replace "test" with "****"
so I get "****"
My code
System.out.println("_test_");
System.out.println("_test_".replaceAll("test", "*"));
But it replace test with 1 *.

If the word test is just an example, you may use Matcher.appendReplacement (see How to appendReplacement on a Matcher group instead of the whole pattern? for more details on this technique):
String fileText = "_test_";
String pattern = "test";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(fileText);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, repeat("*", m.group(0).length()));
}
m.appendTail(sb); // append the rest of the contents
System.out.println(sb);
And the repeat function (borrowed from Simple way to repeat a String in java, see other options there) SO post is:
public static String repeat(String s, int n) {
if(s == null) {
return null;
}
final StringBuilder sb = new StringBuilder(s.length() * n);
for(int i = 0; i < n; i++) {
sb.append(s);
}
return sb.toString();
}
See IDEONE demo

If you have an arbitrary text to be replaced, and you want to use replaceAll(), be aware that it takes a regular expression, and various characters have special meaning. To prevent issues, call Pattern.quote().
Also, to replace with a sequence of * of equal length, you need to build a string of such.
Here is a nice short method for doing it:
private static String mask(String input, String codeword) {
char[] buf = new char[codeword.length()];
Arrays.fill(buf, '*');
return input.replaceAll(Pattern.quote(codeword), new String(buf));
}
Test
System.out.println(mask("_test_", "test"));
System.out.println(mask("This is his last chance", "is"));
Output
_****_
Th** ** h** last chance

Yes, because replaceAll(str1, str2) will replace all occurrences of str1 with str2. Since you are using literals, you need to say
System.out.println("_test_".replaceAll("test", "****"));
If you want your own replacement function you can do something like this:
public static String replaceStringWithChar(String src, String seek, char replacement)
{
StringBuilder sb = new StringBuilder();
for(int i = 0; i < seek.length(); i++) sb.append(replacement);
return src.replaceAll(seek, sb.toString());
}
You would then call it like so:
replaceStringWithChar("_test_", "test", '*');

So I got the answer and I was really looking for
something with as few line as possible. Thank you all
for the answer but this is the answer I found most useful.
I apologize for not being clear in the question, if I was not.
String str1 = "_AnyString_";
int start_underscore = str1.indexOf("_");
int end_underscore = str1.indexOf("_", start_underscore + 1);
String str_anything = str1.substring(start_underscore + 1, end_underscore);
String str_replace_asterisk = str_anything.replaceAll(".", "*");
System.out.println(str_replace_asterisk);
str1 = str1.replace(str_anything, str_replace_asterisk);
System.out.println(str1);
Output:
_AnyString_
_*********_

Actually you are pretty close the what you want. This is what you can do:
System.out.println("_test_".replaceAll("[test]", "*"));
System.out.println("hello".replaceAll("[el]", "*"));
Output:
_****_
h***o

Related

Parse string value from URL

I have a string (which is an URL) in this pattern https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
now I want to clip it to this
media/93939393hhs8.jpeg
I want to remove all the characters before the second last slash /.
i'm a newbie in java but in swift (iOS) this is how we do this:
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString) // "output = media/93939393hhs8.jpeg"
}
Basically, I'm removing everything from this Url expect of last 2 item and then.
I'm joining those 2 items using /.
String ret = url.substring(url.indexof("media"),url.indexof("jpg"))
Are you familiar with Regex? Try to use this Regex (explained in the link) that captures the last 2 items separated with /:
.*?\/([^\/]+?\/[^\/]+?$)
Here is the example in Java (don't forget the escaping with \\:
Pattern p = Pattern.compile("^.*?\\/([^\\/]+?\\/[^\\/]+?$)");
Matcher m = p.matcher(string);
if (m.find()) {
System.out.println(m.group(1));
}
Alternatively there is the split(..) function, however I recommend you the way above. (Finally concatenate separated strings correctly with StringBuilder).
String part[] = string.split("/");
int l = part.length;
StringBuilder sb = new StringBuilder();
String result = sb.append(part[l-2]).append("/").append(part[l-1]).toString();
Both giving the same result: media/93939393hhs8.jpeg
string result=url.substring(url.substring(0,url.lastIndexOf('/')).lastIndexOf('/'));
or
Use Split and add last 2 items
string[] arr=url.split("/");
string result= arr[arr.length-2]+"/"+arr[arr.length-1]
public static String parseUrl(String str) {
return (str.lastIndexOf("/") > 0) ? str.substring(1+(str.substring(0,str.lastIndexOf("/")).lastIndexOf("/"))) : str;
}

Uppercase all characters but not those in quoted strings

I have a String and I would like to uppercase everything that is not quoted.
Example:
My name is 'Angela'
Result:
MY NAME IS 'Angela'
Currently, I am matching every quoted string then looping and concatenating to get the result.
Is it possible to achieve this in one regex expression maybe using replace?
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\'(.*?)\\'");
String input = "'s'Hello This is 'Java' Not '.NET'";
Matcher regexMatcher = regex.matcher(input);
StringBuffer sb = new StringBuffer();
int counter = 0;
while (regexMatcher.find())
{// Finds Matching Pattern in String
regexMatcher.appendReplacement(sb, "{"+counter+"}");
matchList.add(regexMatcher.group());// Fetching Group from String
counter++;
}
String format = MessageFormat.format(sb.toString().toUpperCase(), matchList.toArray());
System.out.println(input);
System.out.println("----------------------");
System.out.println(format);
Input: 's'Hello This is 'Java' Not '.NET'
Output: 's'HELLO THIS IS 'Java' NOT '.NET'
You could use a regular expression like this:
([^'"]+)(['"]+[^'"]+['"]+)(.*)
# match and capture everything up to a single or double quote (but not including)
# match and capture a quoted string
# match and capture any rest which might or might not be there.
This will only work with one quoted string, obviously. See a working demo here.
Ok. This will do it for you.. Not efficient, but will work for all cases. I actually don't suggest this solution as it will be too slow.
public static void main(String[] args) {
String s = "'Peter' said, My name is 'Angela' and I will not change my name to 'Pamela'.";
Pattern p = Pattern.compile("('\\w+')");
Matcher m = p.matcher(s);
List<String> quotedStrings = new ArrayList<>();
while(m.find()) {
quotedStrings.add(m.group(1));
}
s=s.toUpperCase();
// System.out.println(s);
for (String str : quotedStrings)
s= s.replaceAll("(?i)"+str, str);
System.out.println(s);
}
O/P :
'Peter' SAID, MY NAME IS 'Angela' AND I WILL NOT CHANGE MY NAME TO 'Pamela'.
Adding to the answer by #jan_kiran, we need to call the
appendTail()
method appendTail(). Updated code is:
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\'(.*?)\\'");
String input = "'s'Hello This is 'Java' Not '.NET'";
Matcher regexMatcher = regex.matcher(input);
StringBuffer sb = new StringBuffer();
int counter = 0;
while (regexMatcher.find())
{// Finds Matching Pattern in String
regexMatcher.appendReplacement(sb, "{"+counter+"}");
matchList.add(regexMatcher.group());// Fetching Group from String
counter++;
}
regexMatcher.appendTail(sb);
String formatted_string = MessageFormat.format(sb.toString().toUpperCase(), matchList.toArray());
I did not find my luck with these solutions, as they seemed to remove trailing non-quoted text.
This code works for me, and treats both ' and " by remembering the last opening quotation mark type. Replace toLowerCase appropriately, of course...
Maybe this is extremely slow; I don't know:
private static String toLowercaseExceptInQuotes(String line) {
StringBuffer sb = new StringBuffer(line);
boolean nowInQuotes = false;
char lastQuoteType = 0;
for (int i = 0; i < sb.length(); ++i) {
char cchar = sb.charAt(i);
if (cchar == '"' || cchar == '\''){
if (!nowInQuotes) {
nowInQuotes = true;
lastQuoteType = cchar;
}
else {
if (lastQuoteType == cchar) {
nowInQuotes = false;
}
}
}
else if (!nowInQuotes) {
sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
}
}
return sb.toString();
}

Is there a way to use replaceAll on string but call method for replacing the text on each occurrence of a match

I want to replace all occurrences of particular string with different UUID's. For example,
content = content.replaceAll("xyz", "xyz" + generateUUID());
but problem here is that all the "xyz"'s will get replaced by same UUID. But I want that each "xyz" gets replaced by an individual unique ID. How can this be done?
You can do this using Matcher.appendReplacement. This will give you the replaceAll functionality of a complete regex (not just a static String). Here, I use uidCounter as a very simple generateUUID; you should be able to adapt this to your own generateUUID function.
public class AppendReplacementExample {
public static void main(String[] args) throws Exception {
int uidCounter = 1000;
Pattern p = Pattern.compile("xyz");
String test = "abc xyz def xyz ghi xyz";
Matcher m = p.matcher(test);
StringBuffer sb = new StringBuffer();
while(m.find()) {
m.appendReplacement(sb, m.group() + uidCounter);
uidCounter++;
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
Output:
abc xyz1000 def xyz1001 ghi xyz1002
You could use a StringBuilder (for efficiency, since String is immutable), a while loop and something like
// content = content.replaceAll("xyz", "xyz" + generateUUID());
StringBuilder sb = new StringBuilder(content);
String toReplace = "xyz";
int toReplaceLen = toReplace.length();
int pos;
while ((pos = sb.indexOf(toReplace)) > -1) {
sb.replace(pos, pos + toReplaceLen, generateUUID());
}
// content = sb.toString(); // <-- if you want to use content.
It looks like you'd like a way to say something like this:
content = content.replaceAll("xyz", x -> x + generateUUID());
Here's an adaptation of durron597's answer that lets you do almost that:
content = replaceAll(content, "xyz", x -> x + generateUUID());
public static String replaceAll(String source, String regex,
Function<String, String> replacement) {
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
matcher.appendReplacement(sb, replacement.apply(matcher.group(0)));
}
matcher.appendTail(sb);
return sb.toString();
}

SubString replacement in text

I want to replace all the occurrences of a string in a text except the first one.
for eg:
input: Example [2] This is a sample text. This is a sample text. This is a sample text.
replaced word: sample (sImple)
output: Example [2] This is a sample text. This is a sImple text. This is a sImple text.
In string functions what I see is replace, replaceAll, replaceFirst.
How should I handle this case.
Thanks in advance.
You can use this regex to search:
((?:\bsample\b|(?<!^)\G).*?)\bsample\b
And this for replcement:
$1simple
RegEx Demo
Java Code:
String r = input.replaceAll("((?:\\bsample\\b|(?<!^)\\G).*?)\\bsample\\b", "$1simple");
replaceAll and replace will replace all substrings (difference between them is that replaceAll uses regular expression as argument, while replace uses literals).
replaceFirst will replace only first substring which will match pattern you want to find.
What you can do is
use indexOf(String str, int fromIndex) method to determine indexes of first and second sample word,
then substring(int beginIndex) on index of second sample to get part of string from which you want to let replacing possible
and call your replace method on this part
when replacement is done you can concatenate part which shouldn't be changed (before index of second sample word) and part with replaced values
Other solution would be using appendReplacement and appendTail form Matcher class and use replacing value after you find second sample word. Code for it can look like
String yourString = "Example [2] This is a sample text. This is a sample text. This is a sample text.";
Pattern p = Pattern.compile("sample", Pattern.LITERAL);
Matcher m = p.matcher(yourString);
StringBuffer sb = new StringBuffer();
boolean firstWordAlreadyFound = false;
while (m.find()) {
if (firstWordAlreadyFound) {
m.appendReplacement(sb, "sImple");
} else {
m.appendReplacement(sb, m.group());
firstWordAlreadyFound = true;
}
}
m.appendTail(sb);
String result = sb.toString();
System.out.println(result);
Output:
Example [2] This is a sample text. This is a sImple text. This is a sImple text.
Here is a naive approach:
public static String replaceAllButFirst(String text, String toReplace, String replacement) {
String[] parts = text.split(toReplace, 2);
if(parts.length == 2) { //Found at least one match
return parts[0] + toReplace + parts[1].replaceAll(toReplace, replacement);
} else { //no match found giving original text
return text;
}
}
public static void main(String[] args) {
String x = "This is a sample test. This is a sample test. This is a sample test";
System.out.println(replaceAllButFirst(x, "sample", "simple"));
}
Which will give:
This is a sample test. This is a simple test. This is a simple test
Try with substring and indexOf methods to break it in two string then replace in second string and finally append both the strings back
sample code:
String str = "Example [2] This is a sample text. This is a sample text. This is a sample text.";
String findWhat = "sample";
int index = str.indexOf(findWhat) + findWhat.length();
String temp = str.substring(0, index + 1); // first string
str = str.substring(index + 1); // second string
//replace in second string and combine back
str = temp + str.replace(findWhat, "simple"); // final string
System.out.println(str);
combine all in few statements:
int index = str.indexOf(findWhat) + findWhat.length();
str = str.substring(0, index + 1) + str.substring(index + 1).replace(findWhat, "simple");
There is no built-in function that does exactly what you want, either in the String or StringBuilder classes. You'll need to write your own. Here's a quickie:
private string ReplaceText(string originalText, string textToReplace, string replacementText)
{
string tempText;
int firstIndex, lastIndex;
tempText = originalText;
firstIndex = originalText.IndexOf(textToReplace);
lastIndex = tempText.LastIndexOf(textToReplace);
while (firstIndex >= 0 && lastIndex > firstIndex)
{
tempText = tempText.Substring(0,lastIndex) + replacementText + tempText.Substring(lastIndex + textToReplace.Length);
lastIndex = tempText.LastIndexOf(textToReplace);
}
return tempText;
}
Another option:
(?<=\bsample\b)(.*?)\bsample\b
And replacement:
$1yourstring
Java Code:
String s=input.replaceAll("(?<=\\bsample\\b)(.*?)\\bsample\\b", "$1yourString");

Insert a character before and after all letters in a string in Java

I want to insert a % character before after every letter in a string, but using StringBuilder to make it fast.
For example, if a string is 'AA' then it would be '%A%A%'. If it is 'XYZ' then it would be '%X%Y%Z%'
String foo = "VWXYZ";
foo = "%" + foo.replaceAll("(.)","$1%");
System.out.println(foo);
Output:
%V%W%X%Y%Z%
You don't need a StringBuilder. The compiler will take care of that simple concatenation prior to the regex for you by using one.
Edit in response to comment below:
replaceAll() uses a Regular Expression (regex).
The regex (.) says "match any character, and give me a reference to it" . is a wildcard for any character, the parenthesis create the backreference. The $1 in the second argument says "Use backreference #1 from the match".
replaceAll() keeps running this expression over the whole string replacing each character with itself followed by a percent sign, building a new String which it then returns to you.
Try something like this:
String test = "ABC";
StringBuilder builder = new StringBuilder("");
builder.append("%");
for (char achar : test.toCharArray()) {
builder.append(achar);
builder.append("%");
}
System.out.println(builder.toString());
public static String escape(String s) {
StringBuilder buf = new StringBuilder();
boolean wasLetter = false;
for (char c: s.toCharArray()) {
boolean isLetter = Character.isLetter(c);
if (isLetter && !wasLetter) {
buf.append('%');
}
buf.append(c);
if (isLetter) {
buf.append('%');
}
wasLetter = isLetter;
}
return buf.toString();
}
StringBuilder sb = new StringBuilder("AAAAAAA");
for(int i = sb.length(); i >= 0; i--)
{
sb.insert(i, '%');
}
You may see this.
String s="AAAA";
StringBuilder builder = new StringBuilder();
char[] ch=s.toCharArray();
for(int i=0;i<ch.length;i++)
{
builder.append("%"+ch[i]);
}
builder.append("%");
System.out.println(builder.toString());
Output
%A%A%A%A%
I agree with #Brian Roach to add character to before and after but if you want to add any specific character then do like this
String source = "hello good old world";
StringBuffer res = new StringBuffer();
String[] strArr = tagList.split(" ");
for (String str : strArr) {
char[] stringArray = str.trim().toCharArray();
stringArray[0] = stringArray[0];
str = new String(stringArray);
//here you need to specify your first and last character which you want to set
res.append("#"+ str + "$").append(" ");
}
System.out.println("Result: " + res.toString().trim());
Output :- #hello$ #good$ #old$ #world$

Categories

Resources