2017-06-19 1 views
0

J'ai [email protected] et je veux obtenir le "myword" de cette chaîne comment le faire?asp classique comment prendre un mot ou une première lettre dans une chaîne après un symbole spécifique comme @

trouvé cela, mais ne savent pas comment convertir en asp

String res = email.substring(email.indexOf("@") + 1); 

je sais comment utiliser gauche et à droite len mi division, je crois que la réponse est un jeu de ces fonctions mais je trouve pas répondre à ma recherche de comment.

aussi si quelqu'un sait comment faire avec regex comme motif (juste commencé à travailler avec) beaucoup apprécié

MERCI pour toute l'aide :)

Répondre

0

a pris un certain temps et quelques essais, mais je trouvé :)

[email protected] 
response.write mid(email,(inStr(email, "@"))+1,1) 
2

Alors que this other answer n'est pas mal et retournera la première lettre après le « @ », il y a une approche plus générique. Pour cela, je vais utiliser une fonction qui divise une chaîne en plusieurs parties, sur la base à la fois « de » et « à » délimiteurs:

Function GetBetween(str, leftDelimeter, rightDelimeter) 
    Dim tmpArr, result(), x 
    tmpArr=Split(str, leftDelimeter) 
    If UBound(tmpArr) < 1 Then 
     GetBetween=Array() : Exit Function 
    End If 
    ReDim result(UBound(tmpArr)-1) 
    For x=1 To UBound(tmpArr) 
     result(x-1)=(Split(tmpArr(x), rightDelimeter))(0) 
    Next 
    Erase tmpArr 
    GetBetween=result 
End Function 

Maintenant, pour l'utiliser dans ce cas specfic, ont ce code:

Dim email, tempArray 
email = "[email protected]" 

'find the word between the "@" and the first dot 
tempArray = GetBetween(email, "@", ".") 

'check that we got anything: 
If UBound(tempArray)<0 Then 
    Response.Write("invalid email") 
Else 
    'desired word is the first item in the array: 
    Response.Write(tempArray(0)) 
End If 

'free allocated memory for dynamic array: 
Erase tempArray