如果字符串包含字符,添加分隔符?

huangapple go评论85阅读模式
英文:

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 = &quot;10584f&quot;;
    		String newString = &quot;&quot;;
    		for(int i=0;i&lt;s.length();i++) {
    			if((int)s.charAt(i)&gt;= (int)&#39;a&#39; &amp;&amp;  (int)s.charAt(i)&lt;= (int)&#39;f&#39;) {
    				newString = newString + &quot;:&quot; + 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 = &quot;10584f&quot;;
		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 &gt;= 65 &amp;&amp; charCode &lt;= 70) || (charCode &gt;= 97 &amp;&amp; charCode &lt;= 102))
				sb.append(&quot;:&quot;);
			sb.append(c);
		}
		return sb.toString();
	}
}

Output:

10584:f

答案3

得分: 0

如果您的目标字符串由数字后跟单个字母(A-Fa-f)组成,一个简单的解决方案可以是:替换正则表达式匹配,(\\d+)([A-Fa-f])。这个正则表达式意味着group(1)包含数字,而group(2)包含A-Fa-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 = { &quot;10584f&quot;, &quot;12345A&quot;, &quot;13456b&quot;, &quot;23456F&quot; };
		for (int i = 0; i &lt; arr.length; i++) {
			arr[i] = arr[i].replaceAll(&quot;(\\d+)([A-Fa-f])&quot;, &quot;$1:$2&quot;);
		}

		// 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 = { &quot;10584f&quot;, &quot;12345A&quot;, &quot;13456b&quot;, &quot;23456F&quot;, &quot;1abc&quot;, &quot;123aBc&quot; };
		for (int i = 0; i &lt; 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 &lt; s.length(); i++) {
			char ch = s.charAt(i);
			if ((ch &gt;= &#39;A&#39; &amp;&amp; ch &lt;= &#39;F&#39;) || (ch &gt;= &#39;a&#39; &amp;&amp; ch &lt;= &#39;f&#39;)) {
				sb.append(&#39;:&#39;).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]

huangapple
  • 本文由 发表于 2020年10月25日 22:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64524625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定