# Convert VariableRef to String

**URL:** <https://discourse.julialang.org/t/convert-variableref-to-string/72803>\
**Category:** Optimization (Mathematical)\
**Created:** [December 9, 2021, 4:17am UTC](https://discourse.julialang.org/t/convert-variableref-to-string/72803 "2021-12-09T04:17:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gopal2017](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gopal2017/32/31747_2.png) [@gopal2017](https://discourse.julialang.org/u/gopal2017)\
**Post date:** [December 9, 2021, 4:17am UTC](https://discourse.julialang.org/t/convert-variableref-to-string/72803/1 "2021-12-09T04:17:04Z")

</div>

We can use string(variable) to convert integer variable to string. Can we convert VariableRef to string? VariableRef we are getting from math model.

I tried with parse(AbstractString,VariableRef) or string(VariableRef) but it is not working. Please check the code mentioned below:

using JuMP  
using GLPK  
using MathOptInterface

const MOI = MathOptInterface  
mathoptformat\_model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT\_MPS)  
FILE\_NAME=“E:\file1.mps”  
MOI.read\_from\_file(mathoptformat\_model, FILE\_NAME)  
m = Model(GLPK.Optimizer)  
MOI.copy\_to(m, mathoptformat\_model)  
optimize!(m)  
println(“Optimal Solution:”,objective\_value(m))

for var in all\_variables(m)  
var\_name = parse(AbstractString, var) # or string(var)  
if var\_name[1] == “C”  
println(var\_name)  
end  
end

Please suggest.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 9, 2021, 4:44am UTC](https://discourse.julialang.org/t/convert-variableref-to-string/72803/2 "2021-12-09T04:44:55Z")

</div>

[https://jump.dev/JuMP.jl/stable/manual/variables/#Variable-names](https://jump.dev/JuMP.jl/stable/manual/variables/#Variable-names)

Use `name(var)`.

You can also simplify a lot of this.

```nohighlight
using JuMP, GLPK
model = read_from_file("E:\file1.mps")
set_optimizer(model, GLPK.Optimizer)
optimize!(model)
println(“Optimal Solution:”, objective_value(model))
for x in all_variables(model)
    println("$(name(x)) = $(value(x))")
end

```

---

<div class="post-metadata">

**Author:** ![gopal2017](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gopal2017/32/31747_2.png) [@gopal2017](https://discourse.julialang.org/u/gopal2017)\
**Post date:** [December 9, 2021, 9:40am UTC](https://discourse.julialang.org/t/convert-variableref-to-string/72803/3 "2021-12-09T09:40:07Z")

</div>

Thank you very much.
