In a string to format (mostly to replace chars with different symbols for rendering test on UI), I have to detect % and then skip all chars util first space from this % char and it has to be repeated for all instances in the string.
E.g. abcd%1$s efgh %2$d ijkl
.In this string, I have to get index of % and then find index of first space from that. Basically, I have to skip this %1$s
& %2$d
which are some sort of formatting placeholders. I hope, I am not putting it in complex way here.
int indexOf(String str, int strt) : This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned. Syntax: int indexOf(String str, int strt) Parameters: strt: the index to start the search from.
Use scan. nextLine() because scan. next() will read until it encounters a white space (tab, space, enter) so it finish getting when it see space. You yourself could guess this by printing s, too! be successful!
In C#, IndexOf() method is a string method. This method is used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found.
IndexOf( ) Method. The String. IndexOf() method in C# is used to find the zero-based index of the first occurrence of a specified Unicode character or string within this instance.
You can get that pretty easily, just grab the index if the first percent sign and then leverage that index to find the first space from there:
var start = myString.IndexOf("%");
var spaceIndex = myString.IndexOf(" ", start)
of course the value of myString is the string you represented in your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With