mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
In C# format strings positions are indexed from 0, in Java they are indexed from 1
This commit is contained in:
parent
76bb9265ca
commit
260de07443
@ -24,6 +24,8 @@ package CS2JNet.System;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class StringSupport {
|
||||
|
||||
@ -365,7 +367,17 @@ public class StringSupport {
|
||||
// Takes a C# format string and converts it to a Java format string
|
||||
public static String CSFmtStrToJFmtStr(String fmt)
|
||||
{
|
||||
return fmt.replaceAll("\\{(\\d)+\\}", "%$1\\$s");
|
||||
Pattern p = Pattern.compile("\\{(\\d)+\\}");
|
||||
Matcher m = p.matcher(fmt);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (m.find()) {
|
||||
// In C# positions are indexed from 0, in Java they are indexed from 1
|
||||
// so "{0} = {1}" -> "%1$s = %2$s"
|
||||
String replacement = "%" + String.valueOf(Integer.parseInt(m.group(1)) + 1) + "\\$s";
|
||||
m.appendReplacement(sb, replacement);
|
||||
}
|
||||
m.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user