Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of first detected space after a certain index in a string

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.

like image 681
Indigo Avatar asked Apr 05 '13 10:04

Indigo


People also ask

How do you index the first space of a string?

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.

How to find index of next space in a string in java?

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!

How to get first index of string in c#?

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.

How to find the index of a char in a string c#?

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.


1 Answers

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.

like image 96
Mike Perrenoud Avatar answered Oct 20 '22 03:10

Mike Perrenoud