Home > Back-end >  Get last word of string that starts with 5555/ and ends with 0/
Get last word of string that starts with 5555/ and ends with 0/

Time:03-18

I have a string containg double data that is being sent by sockets. Due to network delay I get overloaded data on the client side, meaning my actual string is

5555/57.6626/63.364/0/

and I get this string on client side:

5555/989.994/262.65645/0/5555/165.6515/6526.545/0/

So basically two strings are merged. I want the last updated string that is in bold format.

Note that 5555/ and /0/ are the delimiters, my actual data is between these delimiters.

CodePudding user response:

You could use a regular expression:

Regex.Matches(input, @"(?<=(^|/)5555/)[\d\./] ?(?=/0(/|$))")

Explanation:

  • (?<=(^|/)5555/) will check for "5555/" at the beginning of the input or after a "/", but not include this in the match
  • [\d\./] ? will match any sequence of digits, dots and slashes (the ? is for a non-greedy match, i.e. the shortest match possible)
  • (?=/0(/|$)) will check for "/0" at the end of the input or preceding a "/", but not include this in the match

This will produce two matches "989.994/262.65645" and "165.6515/6526.545"; just take the last one.

CodePudding user response:

var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Substring(s.LastIndexOf("5555/")   5).
    Substring(0, s.LastIndexOf("0/") - s.LastIndexOf("5555") - 5);

result:

165.6515/6526.545/

CodePudding user response:

another variant

var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Split("5555/").Last().Split("0/")[0];

C# 8.0

var x = s.Split("5555/")[^1].Split("0/")[0];
  • Related