1

I am trying to convert a string to an array then back to a string again. I am trying to achieve this in C# but as i have not done C# in a while i am having issues. I created the following code in Java and it works fine:

String sHtml = "test1\r\ntest2\r\ntest3\r\ntest4\r\ntes5t\r\ntest6\r\ntest7\r\ntest8\r\ntest9\r\ntest10\r\ntest11\r\ntest12\r\ntest13\r\ntes14t\r\n";

    int temp = 0;
    List<String> emailText = new ArrayList<String>();

    for(int x = 0; x<sHtml.length();x++){
        if(sHtml.charAt(x)=='\n'){
            emailText.add(sHtml.substring(temp, x));
            temp = x;
        }
    }

    String testingString="";

    for(String words:emailText){
        //System.out.println(words);
        testingString+=words;


    }

    System.out.println(testingString);

This works fine in Java. The following code is what i have for C#:

int temp = 0; 
List<string> emailText = new List<string>();   

for (int x = 0; x < sHtml.Length; x++) 
{ 
    if (sHtml[x].Equals("\\n")) 
    { 
        emailText.Add(sHtml.Substring(temp, x)); 
        temp = x; 
    } 
    else 
    { 
    } 
} 
string testingString = ""; 
//sHtml = string.Join("\r\n", emailText.ToArray()); 
foreach (String word in emailText) 
{ 
     testingString += word; 
}

Console.WriteLine(testingString);

The java code outputs fine but i am getting no output from the C# code. I have a feeling i am missing something small from the C# code but i am not sure what, Can someone please help?

Thanks in advance

5
  • 1
    You have 2 slashes in "if (sHtml[x].Equals("\\n"))" Commented Nov 19, 2014 at 11:45
  • i am getting no output from the C# code Because you aren't printing. Add Console.writeLine(testingString) at the end Commented Nov 19, 2014 at 11:46
  • 1
    why dont you use String.split() instead? Commented Nov 19, 2014 at 11:46
  • You're missing a Console.WriteLine() statement, right ? Commented Nov 19, 2014 at 11:47
  • sorry about that, i missed the writeline statement when copying the code over. This is a test program to make sure the text is returned correctly. The overall goal is to pass html code into the string sHtml and then split the lines at the newline character and then format correctly and return to a string. Commented Nov 19, 2014 at 11:59

5 Answers 5

2

You don't get output in C# because you don't output anything :-) You omitted the Java System.out.println statement without adding the C# equivalent:

Console.WriteLine(testingString);

BTW: Once you're replacing your Java code by C# code, you can also make use of the .NET framework's features (as others already mentioned). This way you can reduce your program to one line:

Console.WriteLine(string.Join(string.Empty, sHtml.Split('\n')));
Sign up to request clarification or add additional context in comments.

2 Comments

The last line give me an error. But Console.WriteLine(string.Join(string.Empty, sHtml.Split(new string[] { "\\n" }, StringSplitOptions.None))); work fine for me
@deltonio2, you're right, my brain's own syntax checker is still incomplete yet :-)
1

Try this: although i would recommend using a string builder for larger strings as they're immutable.

        string yourString = "this,is,a,example,string";
        string newString = "";

        string[] array = yourString.Split(',');

        foreach (string s in array)
        {
            newString += s;
        }

        Console.WriteLine(newString);

1 Comment

the foreach should be replaced with var newString = String.Join("",array);
1

Why don't you use this to split the string:

        string[] List = sHtml.split("\\n");   

And this to do something with the arraylist of strings to do something:

        for (String s in List){ 
                //Do something with each separate String s
            } 

Comments

1

Why not use split and join?

var arr = str.Split('\n');
var newStr = string.Join("", arr);

Comments

0

Your 'for' loop has 2 mistakes - you should keep the character '\n' and Java 'substring' doesn't have the same second parameter that .NET 'Substring' has, so you need to adjust that:

for (int x = 0; x < sHtml.Length;x++)
{
    if (sHtml[x] == '\n')
    {
        emailText.Add(sHtml.Substring(temp, x - temp));
        temp = x;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.