如何从 String2 中删除 String1 并用剩余的字符串构建新的 String2?

发布于 2024-11-05 12:55:30 字数 215 浏览 1 评论 0原文

我有 2 个字符串 str1 和 str2,我想构建一个更新的 str2,其中包含 str1 中不存在的字符的内容,而不使用字符串的内置函数。 字符串如下:

String str1="bdf";

String str2="abc gde fhi**";

输出应如下所示:

"ac ge hi";

I have 2 String str1 and str2 and i want to build a updated str2 with the contents which character is not present inside the str1 without using inbuilt function of string.
String is as follows:

String str1="bdf";

String str2="abc gde fhi**";

and output should be like:

"ac ge hi";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-11-12 12:55:30

我想说使用内置方法从字符串中读取字符数组,并使用 foreach 循环来删除包含的字符。

对于 c# ,它看起来像这样:

foreach(char c in  str1)
{
   str2.Replace(c,' ').Trim();
}

当然,您也可以使用索引和 str.Remove() 来避免空格...

-编辑抱歉,我刚刚读到您不允许使用内置函数 - 但从字符串读取数组是没有函数 - 每个字符串都作为字符数组保存在内存中 - 所以这应该没问题,只需更改删除字符的方式:

String result;
int i=0;
foreach(char c in  str2)
{
   Bool isincluded = false;

   foreach(char c2 in str1)
   {
     if(c == c2) isincluded = true;
   }

   if(isincluded == false)
   { 
      result[i] = c;
      i++;
   }
}

尚未验证......但我希望它有效:)

I'd say use the inbuilt way to read from a string a an array of chars and a foreach loop to remove the included chars.

For c# it would look like this:

foreach(char c in  str1)
{
   str2.Replace(c,' ').Trim();
}

Of course you could also use indexes and str.Remove() to avoid spaces...

-EDIT sorry I just read you weren't allowed to use inbuilt functions - but reading from string as array is no function - every string is saved in memory as an array of chars - so this should be ok, only got to change the way to remove the chars:

String result;
int i=0;
foreach(char c in  str2)
{
   Bool isincluded = false;

   foreach(char c2 in str1)
   {
     if(c == c2) isincluded = true;
   }

   if(isincluded == false)
   { 
      result[i] = c;
      i++;
   }
}

isn't verified...but I hope it works :)

吲‖鸣 2024-11-12 12:55:30
String removeCharsFromString(String fromString, String charsToBeRemoved) {
    BitSet charSet = new BitSet();
    for (char c : charsToBeRemoved.toCharArray())
        charSet.set(c);

    StringBuilder outputStr = new StringBuilder();
    for (char c : fromString.toCharArray())
        if (charSet.get(c) == false)
            outputStr.append(c);
    return outputStr.toString();
}
String removeCharsFromString(String fromString, String charsToBeRemoved) {
    BitSet charSet = new BitSet();
    for (char c : charsToBeRemoved.toCharArray())
        charSet.set(c);

    StringBuilder outputStr = new StringBuilder();
    for (char c : fromString.toCharArray())
        if (charSet.get(c) == false)
            outputStr.append(c);
    return outputStr.toString();
}
计㈡愣 2024-11-12 12:55:30

这是完整的示例。这不是您可以获得的最佳效果,但由于您无法使用专门针对您的问题制作的函数,因此这应该可以正常工作:

import java.util.*;
public class Test{
    public static void main(String[]args){
        String s1 = "abc def ghi"; // Source
        String s2 = "behi";           // Chars to delete 

        StringBuffer buf = new StringBuffer(s1); // Creates buffer (for deleteCharAt() func)

        for (int i=0; i<s2.length(); i++){ // For each letter in the delete-string 
            for (int j=0; j<s1.length(); j++){ // Test each letter in the source string
                if (s1.charAt(j)==s2.charAt(i)){ // Chars equal -> delete
                    buf.deleteCharAt(j);
                    s1=buf.toString(); // Updates the source string
                    j--;     // Moves back one position (else it would skip one char, due to its deleting)
                }
            }
        }
        System.out.println(buf.toString());
    }
}

This is the full example. It's not the best effective you can get, but since you can't use functions made especially for your problem, this should work fine:

import java.util.*;
public class Test{
    public static void main(String[]args){
        String s1 = "abc def ghi"; // Source
        String s2 = "behi";           // Chars to delete 

        StringBuffer buf = new StringBuffer(s1); // Creates buffer (for deleteCharAt() func)

        for (int i=0; i<s2.length(); i++){ // For each letter in the delete-string 
            for (int j=0; j<s1.length(); j++){ // Test each letter in the source string
                if (s1.charAt(j)==s2.charAt(i)){ // Chars equal -> delete
                    buf.deleteCharAt(j);
                    s1=buf.toString(); // Updates the source string
                    j--;     // Moves back one position (else it would skip one char, due to its deleting)
                }
            }
        }
        System.out.println(buf.toString());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文