Manipulating Java strings

At the beginning

I will write about the operation of a slightly complicated string

Thing you want to do

For example a b c triple 4649 bd d op When there was a character string triple 4649 I don't want to trim the half-width space included in, but I want to trim the space of other characters In other words abc triple 4649 bddop I want to do something like that

Image of implementation

a b c triple 4649 bd d op To

a b c
 triple 4649 
bd d  op 

Hold it vertically like "a b c"When"bd d op " Trims a half-width space, triple 4649 Try to adopt the policy of not trimming half-width spaces For that purpose, it is necessary to set rules regarding the decomposition and replacement of character strings, which will be explained below.

Rule

The rules for decomposing and replacing character strings are as follows.

A delimiter for listing strings'!',`, The strings you don't want to trim@Enclose in

In the above example

a b c!@ triple 4649 @!bd d op Becomes

Java implementation

I tried to implement it very simply using StreamAPI

ReplaceUtil.java


package stringutil;
import java.util.stream.Stream;

public class ReplaceUtil {
	
	public static String replacewhiteSpace(String target,String separator,String marker) {
		return Stream.of(target.split(separator))
		.map(s->replacer(s,marker))
		.reduce((s1,s2)->s1+s2 )
		.orElse("");
	}
	
	private static  String replacer(String target,String marker) {
		if(target.matches(String.format("%s[\\w| ].*%s", marker,marker))) {
			return target.replaceAll(String.format("%s([\\w| ].*)%s", marker,marker), "$1");
		}else {
			return target.replaceAll(" ", "");
		}
	}

}

Recommended Posts

Manipulating Java strings
Sort strings functionally with java
Java
Java
[Java] Remove whitespace from character strings
How to concatenate strings in java
Java learning (0)
Studying Java ―― 3
Java protected
Convert Java org.w3c.dom.Document objects and XML strings
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)