# JID.fromString constructor

JID.fromString(

1. String jid

)

Parses the string `jid` into a JID instance.

## Implementation

    factory JID.fromString(String jid) {
      // Algorithm taken from here: https://blog.samwhited.com/2021/02/xmpp-addresses/
      var localPart = '';
      var domainPart = '';
      var resourcePart = '';

      final slashParts = jid.split('/');
      if (slashParts.length == 1) {
        resourcePart = '';
      } else {
        resourcePart = slashParts.sublist(1).join('/');

        assert(
          resourcePart.isNotEmpty,
          'Resource part cannot be there and empty',
        );
      }

      final atParts = slashParts.first.split('@');
      if (atParts.length == 1) {
        localPart = '';
        domainPart = atParts.first;
      } else {
        localPart = atParts.first;
        domainPart = atParts.sublist(1).join('@');

        assert(localPart.isNotEmpty, 'Local part cannot be there and empty');
      }

      return JID(
        localPart,
        domainPart.endsWith('.')
            ? domainPart.substring(0, domainPart.length - 1)
            : domainPart,
        resourcePart,
      );
    }