Java中的函数是否等同于php的strtr
?
string strtr ( string $str , string $from , string $to )
如果给出三个参数,strtr
函数返回一个字符串副本,其中$from
中每个(单字节)字符的所有出现都已转换为$to
中的相应字符,即,$from[$n]
的每次出现都已替换为$to[$n]
,其中$n
是两个参数中的有效偏移量。
答案 0 :(得分:5)
无法在JDK中找到与strtr
直接等效的内容。在Apache Commons StringUtils中,有StringUtils.replaceChars
String.replace不适合您的情况,除非您在$from
参数中的每个字母使用它,并将其替换为$to
替换。但是,使用StringBuilder实现自己的实用程序函数并自行替换它非常容易。这几乎是StringUtils.replace所做的。
答案 1 :(得分:3)
您可以在一个周期内调用String.replace(char oldChar, char newChar)
。例如:
String from = "abc";
String to = "xyz";
String str = "abc string";
for( int i = 0; i < from.length(); i++ )
str = str.replace(from.charAt(i), to.charAt(i));
答案 2 :(得分:1)
这是我之前写的一个实现。欢迎你。
public static String strtr(String str, String from, String to) {
char[] out = null;
for (int i = 0, len = str.length(); i < len; i++) {
char c = str.charAt(i);
int p = from.indexOf(c);
if (p >= 0) {
if (out == null) out = str.toCharArray();
out[i] = to.charAt(p);
}
}
return out != null ? new String(out) : str;
}
答案 3 :(得分:1)
写了以下两种方法,因为我正在寻找类似于strtr功能的东西。只需将此代码复制并粘贴到您的Java项目中,并使用提供的两种方法(Apache License 2.0):
Map<String, String> replacements = new TreeMap<String, String>();
replacements.put("this", "that");
replacements.put("that", "this");
replacements.put("Nothing much", "Everything");
String example = Strtr.replaceStringUsingMap("Whether this or that, whats the difference? Nothing much.", replacements);
System.out.println(example);
输入字符串:
"Whether this or that, whats the difference? Nothing much."
输出字符串:
"Whether that or this, whats the difference? Everything."
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Two public methods can be used, mimicing strtr in php:
*
* {@link #replaceStringUsingChars(String, String, String)}
*
* {@link #replaceStringUsingMap(String, Map)}
* Apache 2.0 License
*
* @author Basil Musa
*/
public class Strtr {
/**
* Similar to strtr in php, characters in 'from' will be replaced by characters in 'to'
* in the same order character by character.
*
* @param haystack
* @param from
* @param to
* @return String having the replacements made
*/
public static String replaceStringUsingChars(String haystack, String from, String to) {
if (from.length() != to.length()) {
throw new IllegalArgumentException("from and to should have same number of characters.");
}
Map<String, String> replacements = new HashMap<String, String>();
int length = from.length();
for (int i = 0; i < length; i++) {
replacements.put(String.valueOf(from.charAt(i)), String.valueOf(to.charAt(i)));
}
return replaceStringUsingMap(haystack, replacements);
}
/**
* This method will return a string by replacing all occurrences of keys in needle2replacement occurring in
* haystack with the corresponding values. Once a replacement is made, it wont be replaced again.
*
* The method will start with the longest keys first.
* Keys of "" (empty strings) will be ignored.
*
* If the map is empty, the method returns the same haystack String directly.
*
* @param haystack The string to be replaced
* @param needle2replacement The map of keys to search and values to be replaced
* @return String containing haystack with all the replacements in needle2replacement made
*/
public static String replaceStringUsingMap(String haystack, Map<String, String> needle2replacement)
{
// To optimize, just return the haystack if the map has no items
if (needle2replacement.isEmpty()) {
return haystack;
}
// Sort map by length of keys please by longest string first
Map<String, String> sortedNeedle2replacement = sortKeysByLengthDescending(needle2replacement);
Set<String> arrNeedles = sortedNeedle2replacement.keySet();
List<Range> ranges = new ArrayList<>();
int lengthDifference = 0;
for (String needle : arrNeedles) {
int indexOf = -1;
// Ignore needles whom are empty strings
if (needle.equals("")) {
continue;
}
while(-1 != (indexOf = haystack.indexOf(needle, indexOf + 1))) {
String replacement = sortedNeedle2replacement.get(needle);
if (replacement == null) {
throw new IllegalArgumentException("Map value of null found in key ['" + needle + "']");
}
Range range = new Range(indexOf, indexOf + needle.length() - 1, needle, replacement);
// Check for any overlaps
boolean addRange = true;
for (Range target : ranges) {
if (range.isOverlappingWith(target)) {
addRange = false;
break;
}
}
if (addRange) {
ranges.add(range);
lengthDifference += range.replacement.length() - range.needle.length();
}
}
}
// Sort ranges please
Collections.sort(ranges, new Comparator<Range>() {
@Override
public int compare(Range o1, Range o2) {
return o1.begin - o2.begin;
}
});
// type them
StringBuilder stringBuilder = new StringBuilder(haystack.length() + lengthDifference);
int startAt = 0;
for (Range range : ranges) {
stringBuilder.append(haystack.substring(startAt, range.begin));
stringBuilder.append(range.replacement);
startAt = range.end + 1;
}
stringBuilder.append(haystack.substring(startAt));
return stringBuilder.toString();
}
private static class Range
{
public Range(int begin, int end, String needle, String replacement) {
super();
if (begin > end) {
throw new RuntimeException("begin [" + begin + "] should be smaller or equal to end [" + end + "]");
}
this.begin = begin;
this.end = end;
this.needle = needle;
this.replacement = replacement;
}
public final int begin;
public final int end;
public final String needle;
public final String replacement;
public String toString() {
return String.format("{%d,%d,%s,%s}", begin, end, needle, replacement);
}
/**
* @param target
* @return
*/
public boolean isOverlappingWith(Range target) {
final boolean liesBefore = this.end < target.begin;
final boolean liesAfter = this.begin > target.end;
if (liesBefore || liesAfter) {
return false;
}
return true;
}
/**
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* XXXXXX
* YYYYYYYYYYYYYYYY
*
* Y is surround X as shown above
*
* @param target
* @return
*/
public boolean isSurrounding(Range target) {
if (this.begin < target.begin && this.end > target.end) {
return true;
}
return false;
}
}
private static Map<String, String> sortKeysByLengthDescending(
Map<String, String> needle2replacement) {
Map<String, String> sortedNeedle2replacement = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (s1.length() > s2.length()) {
return -1;
} else if (s1.length() < s2.length()) {
return 1;
} else {
return s1.compareTo(s2);
}
}
});
sortedNeedle2replacement.putAll(needle2replacement);
return sortedNeedle2replacement;
}
}
答案 4 :(得分:0)
我的方法:
public static String strtr(String str, String froms, String tos)
{char[] str1=str.toCharArray();
int j;
for (int i=0,len=str.length(); i<len; ++i)
{j=froms.indexOf(str.charAt(i));
if (j!=-1)
{str1[i]=tos.charAt(j);
}
}
return String.valueOf(str1);
}