You can do this with the getDescribe() method. Below is a sample code for your reference
String SobjectApiName = 'Account';
String Nameofthefield;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
for(String fieldName : fieldMap.keyset() )
{
system.debug('fieldName->>'+fieldName); // This will give you the api name of the field name.
if(fieldName == 'xyz') {
Nameofthefield = fieldName;
}
}
// Once you have the api name, you can copy it as a value to the other field
Contact con = [Select Field1__c from Contact];
con.Field1__c = Nameofthefield;
If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!
Can you describe more of what you're trying to accomplish? You can use the Schema class as Anudeep mentioned, you can also serialize the object and then deserilize to a map of<string,value> and then use the keys from that map.
If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!
Anudeep