英文:
Add delimiter if String contains a character?
问题
我正在尝试弄清楚如何使用循环,在找到字符串s包含字母A-F之前,将分隔符“:”附加到字符串s。
如果我有s = "10584f",那么我希望输出为"10584:f"。
我应该如何处理这个?
英文:
I'm trying to figure out how to use a loop to append the delimiter ":" before the String s is found to contain a letter A-F.
If I have s = "10584f" then I would want the output to be "10584:f"
How should I go about doing this?
答案1
得分: 1
这是一个简单的解决方案,它会检查字符串中是否包含介于 'a' 和 'f' 之间的字母,并相应地添加 :
。
public static void main(String[] args) {
String s = "10584f";
String newString = "";
for (int i = 0; i < s.length(); i++) {
if ((int) s.charAt(i) >= (int) 'a' && (int) s.charAt(i) <= (int) 'f') {
newString = newString + ":" + s.charAt(i);
} else {
newString = newString + s.charAt(i);
}
}
System.out.println(newString);
}
英文:
This is a simple solution that checks if it contains letters between a to f and put `:' accordingly.
public static void main(String[] args) {
String s = "10584f";
String newString = "";
for(int i=0;i<s.length();i++) {
if((int)s.charAt(i)>= (int)'a' && (int)s.charAt(i)<= (int)'f') {
newString = newString + ":" + s.charAt(i);
}else {
newString = newString + s.charAt(i);
}
}
System.out.println(newString);
}
答案2
得分: 0
你需要检查每个字符的ASCII
代码,并在以下条件下在其前面添加分隔符:
public class Main {
public static void main(String[] args) {
String s = "10584f";
System.out.println(appendDelimiter(s));
}
private static String appendDelimiter(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
int charCode = (int) c;
if ((charCode >= 65 && charCode <= 70) || (charCode >= 97 && charCode <= 102))
sb.append(":");
sb.append(c);
}
return sb.toString();
}
}
输出:
10584:f
英文:
You need to check for the ASCII
code of each character and append the delimiter before it on this condition:
public class Main {
public static void main(String[] args) {
String s = "10584f";
System.out.println(appendDelimiter(s));
}
private static String appendDelimiter(String s) {
StringBuilder sb = new StringBuilder();
for(char c : s.toCharArray()) {
int charCode = (int) c;
if((charCode >= 65 && charCode <= 70) || (charCode >= 97 && charCode <= 102))
sb.append(":");
sb.append(c);
}
return sb.toString();
}
}
Output:
10584:f
答案3
得分: 0
如果您的目标字符串由数字后跟单个字母(A-F
或a-f
)组成,一个简单的解决方案可以是:替换正则表达式匹配,(\\d+)([A-Fa-f])
。这个正则表达式意味着group(1)
包含数字,而group(2)
包含A-F
或a-f
中的一个字母。可以将正则表达式替换为$1:$2
,其中$1
和$2
分别指定group(1)
和group(2)
。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F" };
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].replaceAll("(\\d+)([A-Fa-f])", "$1:$2");
}
// After replacement
System.out.println(Arrays.toString(arr));
}
}
输出:
[10584:f, 12345:A, 13456:b, 23456:F]
然而,如果字符串可以包含多个字母后跟数字,并且您希望在每个字母前添加:
,您可以从索引1
开始迭代字符串,直到字符串的最后一个字符,如果遇到字母,则在其前面添加:
,就像下面给出的withColonBeforeLetters
函数中所做的那样:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F", "1abc", "123aBc" };
for (int i = 0; i < arr.length; i++) {
arr[i] = withColonBeforeLetters(arr[i]);
}
// After replacement
System.out.println(Arrays.toString(arr));
}
static String withColonBeforeLetters(String s) {
StringBuilder sb = new StringBuilder();
// Put the first letter into sb
sb.append(s.charAt(0));
for (int i = 1; i < s.length(); i++) {
char ch = s.charAt(i);
if ((ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
sb.append(':').append(ch);
} else {
sb.append(ch);
}
}
return sb.toString();
}
}
输出:
[10584:f, 12345:A, 13456:b, 23456:F, 1:a:b:c, 123:a:B:c]
英文:
If your target string consists of digits followed by a single letter from A-F
or a-f
, a simple solution can be: replacement of the regex-match, (\\d+)([A-Fa-f])
. This regex means group(1)
consisting of digits and group(2)
consisting of a letter from A-F
or a-f
. The regex can be replaced with $1:$2
where $1
and $2
specify group(1)
and group(2)
respectively.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F" };
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].replaceAll("(\\d+)([A-Fa-f])", "$1:$2");
}
// After replacement
System.out.println(Arrays.toString(arr));
}
}
Output:
[10584:f, 12345:A, 13456:b, 23456:F]
However, if the string can have multiple letters followed by digits and you want each letter to be prepended with a :
, you can iterate the string starting from the index, 1
until the last character of the string and if you come across a letter, prepend it with a :
as done in the function, withColonBeforeLetters
given below:
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "10584f", "12345A", "13456b", "23456F", "1abc", "123aBc" };
for (int i = 0; i < arr.length; i++) {
arr[i] = withColonBeforeLetters(arr[i]);
}
// After replacement
System.out.println(Arrays.toString(arr));
}
static String withColonBeforeLetters(String s) {
StringBuilder sb = new StringBuilder();
// Put the first letter into sb
sb.append(s.charAt(0));
for (int i = 1; i < s.length(); i++) {
char ch = s.charAt(i);
if ((ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
sb.append(':').append(ch);
} else {
sb.append(ch);
}
}
return sb.toString();
}
}
Output:
[10584:f, 12345:A, 13456:b, 23456:F, 1:a:b:c, 123:a:B:c]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论