-3
   String up="hello world";
    String end;
    String w;

    for(int i=0;i<up.length();i++){
      n=0;
        z=3;
        end="hello world";
        w=end.substring(n,z);

        end=end.replace(w,"");
        System.out.println(end);
    }
    System.out.println("-->");

Here i have this string up and i just want to replace each alphabet in string with "" and update the replaced string at the end of loop for example: initially we have up="hello world" after first iteration i want it to be up="ello world"

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
sam
  • 11
  • 1
  • 1
  • convert the string to character array and do it. – Spartan Mar 24 '17 at 11:27
  • 1
    Why C# tag? How it is related to it? – Pradeep Simha Mar 24 '17 at 11:29
  • @thanga i have to do it with substring it is restriction... – sam Mar 24 '17 at 11:30
  • @VeeraKannadiga answer in any (java or c#) will be useful for me.. – sam Mar 24 '17 at 11:31
  • 2
    The inside of your loop does the same in every iteration. The value of end is set to "hello world" every time it is entered and then the same operations are performed, regardless of what i is. The result is that you print out the same thing up.length() many times. – Aziuth Mar 24 '17 at 11:32
  • @Aziuth you mean i have to remove end="hello world" for inside of loop? – sam Mar 24 '17 at 11:37
  • Sorry, but if you need to ask, then what you should do is reading some basic tutorials and doing some basic exercises. You lack in some fundamental understanding of how programs works. Don't want to offend you, sorry if this sounds harsh, but that's how it is. – Aziuth Mar 24 '17 at 12:10

3 Answers3

0

You could try something simple like:

String up="hello world";
String end;

for(int i=0;i<up.length();i++){
    end = up.Substring(i,up.length()-i);
    System.out.println(end);
}
System.out.println("-->");

Be sure to head to https://msdn.microsoft.com/library/system.string(v=vs.110).aspx and learn how all methods and parameters of String class work.

Tom
  • 16,842
  • 17
  • 45
  • 54
ArenaLor
  • 465
  • 6
  • 19
  • @Tom i am not mixing it. I need solution for this problem either in java or c# – sam Mar 24 '17 at 11:39
  • @sam You aren't the author of this answer post, so I didn't wrote to you. – Tom Mar 24 '17 at 11:46
  • I am really sorry; for some reason I was thinking about this question as a c# question; the right way to use this is to replace " end = up.Substring(i,up.length()-i); " with " end = up.substring(i); " – ArenaLor Mar 24 '17 at 11:49
  • It was a Java and C# question and OP said he accepts both, but you shouldn't mix your code blocks with Java and C#. If you want to answer for both languages, then use two seperate blocks. – Tom Mar 24 '17 at 11:54
0

The main problem with your code is that you are assigning end="hello world"; for each iteration, so end is reset each time.

One way of doing it would be like this:

String up = "hello world";

while (!up.isEmpty()) {
  up = up.substring(1);
  System.out.println(up);
}
0

You can do this with char Array

public class MultipleTry {

public static void main(String args[]){

    char c = '\0';
    String up="hello world";

    char[] charArray = up.toCharArray();


    for(int i=0 ; i<up.length();i++){

        charArray[i] = c;
        up = new String(charArray);
        System.out.println(up);
    }       
 }

}

If you have question about bellow line,

char c = '\0';

click here to get more information

Community
  • 1
  • 1
Priyantha
  • 4,839
  • 6
  • 26
  • 46