How to get all the occurrence character on the prefix of string in java in a simple way?

Your code is already very simple. The English description isn't much shorter, so don't worry. With a little bit of rewriting you get: public static String prefixOf(String s, char prefix) { for (int I = 0; I CharAt(i)!

= prefix) { return s. Substring(0, i); } } return s; } This definition is only four real lines long: The method definition is always needed for any algorithm. Doesn't count Counts Counts Counts Only a closing brace, which could be omitted Only a closing brace, which could be omitted Counts Every method has to end with a closing brace.

Doesn't count Compared to your description, this is really short.

Your code is already very simple. The English description isn't much shorter, so don't worry. With a little bit of rewriting you get: public static String prefixOf(String s, char prefix) { for (int I = 0; I CharAt(i)!

= prefix) { return s. Substring(0, i); } } return s; } This definition is only four real lines long: The method definition is always needed for any algorithm. Doesn't count.Counts.Counts.Counts.

Only a closing brace, which could be omitted. Only a closing brace, which could be omitted.Counts. Every method has to end with a closing brace.

Doesn't count. Compared to your description, this is really short.

You may do it without building the result string char-by-char. Instead look for the length of the possible match and then cut from the original string. Int length = 0; while (length CharAt(length) == prefix) { length++; } String concatVal = start.

Substring(0, length).

If you wanted to make it really short, you could use a for loop. ;) – Peter Lawrey May 30 at 17:45 1 @Peter You forgot the "and almost unreadable" ;-) I just don't like empty for-loops. – Howard May 30 at 17:47.

Looks pretty good, one possible change would be for(char ch : start.toCharArray()) { if(ch == prefix) { concatVal += prefix; } else { break; } }.

Why not use a char ch instead of Character object. – Peter Lawrey May 30 at 17:44 Yes your right. Both would work but ill fix it.

– RMT May 30 at 17:46.

You could just extract the result from the original string. String text = "0001"; char prefix = '0'; String result = text; for(int I = 0; I = prefix) { result = text. Substring(0, i); break; } } System.out.

Println(result).

– Howard May 30 at 17:42 @Howard, thank you. Fixed now. – Peter Lawrey May 30 at 17:43.

P.S. Maybe I do not correct understand your description If the first character of the string is not matched with the variable value then it will not store and not look again on the preceding character. Could you explain me this line? Thanks, It is mandatory!

Yes, that is my intention. If ever I found the character on the string from first index I'll keep it and the same goes on the preceding character of the string. – ace May 30 at 18:09 And what will be if first symbol not equal?

Just go to the next or skip this string? – Sergii Zagriichuk May 30 at 18:42.

String start="0001"; String concat=""; String prefix="0"; for(int i=0;iCharAt(i)))){ concat=start. Substring(0, i); break; } } System.out. Println(concat).

How can I get the first two zero on that sample string. The rules here is that, I have a variable which hold a character. Then I need to look on the string, if the first character of the string is same with the variable value.

I need to keep it and then if the second string matches again, concatenate it and so on. If the first character of the string is not matched with the variable value then it will not store and not look again on the preceding character. Though I have already solution but I used about 10 lines of codes to do this.

If there is a more simple way to achieve this, please let me know.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions